Thread: TicTacToe

    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    1
    Rep Power
    0

    TicTacToe


    Hey guys (and gals),
    I have a hw assignment where im suppose to make a tictactoe game and I have it mostly working except the only problem is that when I place my X or O i can put them anywhere. I.E place an X on 1 and an O on 1 and an O appears.
    Code:
     import java.util.Scanner;
    
    public class Assignment11 {
    
    	 static char empty =' ';
    
        public static boolean performTurn(char[][]grid, int player)
        {
        	 // player is 1 or 2
        	Scanner input = new Scanner(System.in);
    
        	int move = input.nextInt();
        	char symbol = ' ';
        	if (player == 1) {
    	  		System.out.println("Pick a number to place your O.");
        		symbol = 'X';
        	}
        		else if (player == 2) {
    
        		System.out.println("Pick a number to place your X.");
        		symbol = 'O';
        	}
    
    
    
        	if (move==1) {
        	  	grid[0][0]=symbol;
        	  	displayGrid(grid);
        	}
        	else if (move==2) {
       			grid[0][1]=symbol;
       			displayGrid(grid);
        	}
        	else if (move==3) {
       			grid[0][2]=symbol;
       			displayGrid(grid);
        	}
        	else if (move==4) {
       			grid[1][0]=symbol;
       			displayGrid(grid);
        	}
        	else if (move==5) {
       			grid[1][1]=symbol;
       			displayGrid(grid);
        	}
        	else if (move==6) {
       			grid[1][2]=symbol;
       			displayGrid(grid);
        	}
        	else if (move==7) {
       			grid[2][0]=symbol;
       			displayGrid(grid);
        	}
        	else if (move==8) {
       			grid[2][1]=symbol;
       			displayGrid(grid);
        	}
        	else if (move==9) {
       			grid[2][2]=symbol;
       			displayGrid(grid);
        	}
        	else {
        		System.out.println();
        		return false;
        	}
        	return true;
        }
    
    	//displaying the grid
       	public static void displayGrid(char[][]grid){
        	for (int row = 0; row <grid.length; row++){
           		for (int column = 0; column < grid[row].length; column++){
           			System.out.print(grid[row][column]+" ");
          		}
         		System.out.println();
       		}
        }
    
       	public static boolean gameOver(char[][]grid) {
        	boolean won=false;
        	for(int i=0;i<2;i++){
    	   		if(grid[i][0]==grid[i][1]&& grid[i][1]==grid[i][2]) {
    	   			won=true;
    	   		}
    		   	for(int j=0;j<2;j++){
    		   		if(grid[0][j]==grid[1][j]&& grid[1][j]==grid[2][j]) {
    		   			won=true;
    		   		}
    	    	}
        	}
        	return won;
    	}
    	  public static void main(String[] args) {
    		System.out.println("Lets play a game of tictactoe X you are up first!");
    
    		char[][]grid = {
    		{'1','2','3'},
    		{'4','5','6'},
    		{'7','8','9'}
    		};
    
    		// while tictactoe has spaces left... loop
    		//  Check if number of moves made is == number of spots on board
    		//  Check after each player makes their move, in case an odd number of spots total
        	while(empty==' '){
    
        		displayGrid(grid);
    
    
        		while ( ! performTurn(grid, 1)) {}
    
    
        		if (gameOver(grid)) {
    
    	   			System.out.println("X You Won!!!!!");
    	   			System.exit(0);
        		}
    
        		while ( ! performTurn(grid, 2)) {}
    
    
        		if (gameOver(grid)) {
    
    	   			System.out.println("O You Won!!!!!");
    	   			System.exit(0);
        		}
         	}
    
        }
    }
  2. #2
  3. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,714
    Rep Power
    348
    i can put them anywhere
    How are you keeping track of where the previously placed Xs and Os are located so that the next play will not go to where there was a previous play made?

IMN logo majestic logo threadwatch logo seochat tools logo