newbie needs c++ help

Discussion in 'Programming' started by izlik, Oct 15, 2008.

  1. #1
    Hello there

    Im trying to make a numbers game and i have gotten so far as to display a 3*3 grid, fil it with the numbers 1-8 and and X in the 9'th grid, and now what i need some help with is getting X to move around, your supposed to move X with W A S D as seen below. When X moves around the grid it's supposed to change position with the current number of the position it moves to, and it's this that i have problem with. Getting it to move, and getting it to switch position wich i hope someone could be very kind to help me with/solve? so far i managed to make it show it's current position on the grid.

    *note: inside goal.txt and start.txt is currently this*

    1 2 3
    4 5 6
    7 8 X

    #include "stdafx.h"
    #include <iostream>  
    #include <fstream>
    #include <string>
    using namespace std; 
    
    void jump(char start[][3],int x, int y);
    
    int main() 
    {
    int yes,abryt,x,y; //deklarerade variabl
    char start[3][3], goal[3][3]; //börjar på mitt grid
    
    
    ifstream infil("start.txt");
    //läser in filen som blivit vald
    infil >> start[0][0] >> start[0][1] >> start[0][2] 
          >> start[1][0] >> start[1][1] >> start[1][2] 
    	  >> start[2][0] >> start[2][1] >> start[2][2];
    //tar reda på vart X är på gridet
    	  	for(int i=0;i<3;i++){
    		for(int j=0;j<3;j++){
    			if (start[i][j] == 'X'){
    			y=i;
    			x=j;
    			cout << "your on Square: " << "Y: " << y << " X: " << x << endl << endl;
              
    			}
    		}
    	}
    //skriver ut mitt start grid
    cout << start[0][0] << " " << start[0][1] << " " << start[0][2] 
         << endl << start[1][0] << " " << start[1][1] << " " << start[1][2] 
    	 << endl << start[2][0] << " " << start[2][1] << " " << start[2][2] 
    	 << endl << endl;
    
    ifstream infil2("goal.txt");
    //läser in mitt målgrid från vald fil
    infil2 >> goal[0][0] >> goal[0][1] >> goal[0][2] 
           >> goal[1][0] >> goal[1][1] >> goal[1][2] 
    	   >> goal[2][0] >> goal[2][1] >> goal[2][2];
    //skriver ut mitt mål grid
    cout << goal[0][0] << " " << goal[0][1] << " " << goal[0][2] 
         << endl << goal[1][0] << " " << goal[1][1] << " " << goal[1][2] << endl 
         << goal[2][0] << " " << goal[2][1] << " " << goal[2][2] << endl << endl;
    
    	 int loop = 0;
    	 while (loop == 0){
    		jump(start, x, y); 
      
    	}
    	 /* system ("cls"); */
    	 
    
    return 0;
    }
    
    void jump(char start[][3], int x, int y){
    string jump2;
    cout << "You move by using W A S D on you keyboard: ";
    cin >> jump2;
    
    
    //funktion för att avbryta spelet
    if(jump2 == "avbryt"){
    	string quit;
        cout << "Want to quit the game eh? Yes/no" ;
        cin >> quit;
    if(quit == "yes"){
    	cout << "bye message" << endl;
    }
    else cout << "stay message";
    
    }
    //if satser för att röra på sej
    if(jump2 == "w"){
    	y--;
    	if (y = 0 ){
    		cout << "lol, you cant move here" << endl;
    	}
    	
    }
    
    if(jump2 == "a"){
    	x--;
    	if (x = 0 ){
    		cout << "lol, you cant move here" << endl;
    	}
    }
    if(jump2 == "s"){
    	y++;
    	if (y = 2 ){
    		cout << "lol, you cant move here" << endl;
    	}
    }
    if(jump2 == "d"){
    	x++;
    	if (x = 2 ){
    		cout << "lol, you cant move here" << endl;
    	}
    }
    	}
    Code (markup):
     
    izlik, Oct 15, 2008 IP