Results 1 to 2 of 2
  1. #1
    _inase is offline Member
    Join Date
    Jan 2011
    Posts
    13
    Rep Power
    0

    Default infamous tictactoe program...

    I'm making a tic-tac-toe program, but I have two issues. First, the code compiles and runs, but the while loop in main never stops. I suppose it's not checking for a winner correctly but I'm unsure how to fix it. Also, how do I get the display board to display the 'X' or 'O' after each input?

    Main
    Java Code:
    import java.util.Scanner;
    
    public class Assignment2 {
    
    	public static void main(String[] args) {
    		
    		Scanner in = new Scanner(System.in);
    		System.out.println("Welcome to Tic-Tac-Toe");
    
    		TicTacToe game = new TicTacToe();
    		
    		int row;
    		int col;
    		
    		while(game.findWinner() == ' ' || game.boardFull()){
    			
    			game.boardDisplay();
    			
    			System.out.print(game.currentPlayer() + " choose a row, 0-2: ");
    			row = in.nextInt();
    	
    			if(row < 0 || row > 2){
    				System.out.print("Invalid. Try again.");
    				row = in.nextInt();}
    			
    			System.out.print("Choose a column, 0-2: ");
    			col = in.nextInt();
    			
    			if(col < 0 || col > 2){
    				System.out.print("Invalid. Try again.");
    				col = in.nextInt();}
    			
    			while(game.setMove(row, col)){
    				System.out.print("Space taken, try again, choose row 0-2: ");
    				row = in.nextInt();
    				System.out.print("Choose column: ");
    				col = in.nextInt();
    			}
    			
    			System.out.print(game.switchPlayer() + " choose a row, 0-2: ");
    			row = in.nextInt();
    			
    			if(row < 0 || row > 2){
    				System.out.print("Invalid. Try again.");
    				row = in.nextInt();}
    			
    			System.out.print("Choose a column, 0-2: ");
    			col = in.nextInt();
    			
    			if(col < 0 || col > 2){
    				System.out.print("Invalid. Try again.");
    				row = in.nextInt();}
    			
    			while(game.setMove(row, col)){
    				System.out.print("Space taken, try again, choose row 0-2: ");
    				row = in.nextInt();
    				System.out.print("Choose column: ");
    				col = in.nextInt();
    			}
    		
    			}
    		
    		if(game.boardFull() == true){
    			System.out.print("It's a draw");
    		}
    		else{
    		System.out.print(game.findWinner() + "WINS!");
    		}
    		
    	}
    }
    Tictactoe class
    Java Code:
    import java.util.*;
    
    public class TicTacToe {
    	
    	Scanner in = new Scanner(System.in);
    
    	char[][] board = new char[3][3];
    	final int ROWS = 3;
    	final int COLUMNS = 3;
    	final char EMPTY = ' ';
    	char player1 = 'X';
    	char player2 = 'O';
    	char currentPlayer;
    	
    	public void init() {    
    
    	        for (int r = 0; r < 3; r++) {
    	            for (int c = 0; c < 3; c++) {
    	                board[r][c] = EMPTY;
    	            }
    	        }
    	}
    	
    	public boolean setMove(int r, int c){
    		
    		boolean move;
    		
    		if(board[r][c] == EMPTY){
    			board[r][c] = currentPlayer;
    			return true;
    		}
    		else{
    			move = false;
    		}
    		return move;
    	}
    	
    	public boolean boardFull(){
    		
    		boolean full = true;
    		
    		for(int r = 0; r < 3; r++){
    			for(int c = 0; c < 3; c++){
    				if(board[r][c] == EMPTY){
    					
    					full = false;
    				}
    			}
    		}
    		return full;
    	}
    	
    	public void boardDisplay(){
    		
    		System.out.println(" "+board[0][0]+" | "+board[0][1]+" | "+board[0][2]);
    		System.out.println(" ---------");
    		System.out.println(" "+board[1][0]+" | "+board[1][1]+" | "+board[1][2]);
    		System.out.println(" ---------");
    		System.out.println(" "+board[2][0]+" | "+board[2][1]+" | "+board[2][2]);
    		
    	}
    	
    	public char findWinner(){
    		
    		char winner = ' ';
    		
    		//check rows
    		while(winner == ' '){
    		if(board[0][0] == board[0][1] && board[0][0] == board[0][2]){
    			if(board[0][0] != ' '){
    				winner = board[0][0];
    				break;
    			}
    		}
    		if(board[1][0] == board[1][1] && board[1][0] == board[1][2]){
    			if(board[1][0] != ' '){
    				winner = board[1][0];
    				break;
    			}
    		}
    		if(board[2][0] == board[2][1] && board[2][0] == board[2][2]){
    			if(board[2][0] != ' '){
    				winner = board[2][0];
    				break;
    			}
    		}
    	}
    		//check columns
    	while(winner == ' '){
    		if(board[0][0] == board[1][0] && board[0][0] == board[2][0]){
    			if(board[0][0] != ' '){
    				winner = board[0][0];
    				break;
    			}
    		}
    		if(board[0][1] == board[1][1] && board[0][1] == board[2][1]){
    			if(board[0][1] != ' '){
    				winner = board[0][1];
    				break;
    			}
    		}
    		if(board[0][2] == board[1][2] && board[0][2] == board[2][2]){
    			if(board[0][2] != ' '){
    				winner = board[0][2];
    				break;
    			}
    		}
    	}
    		//check diagonally
    		if(board[0][0] == board[1][1] && board[0][0] == board[2][2]){
    			if(board[0][0] != ' '){
    				winner = board[0][0];
    			}
    		}
    		if(board[0][2] == board[1][1] && board[0][2] == board[2][0]){
    			if(board[0][2] != ' '){
    				winner = board[0][0];
    			}
    		}
    		return winner;
    	}
    	
    	public char currentPlayer(){
    		currentPlayer = player1;
    		return player1;
    	}
    	
    	public char switchPlayer(){
    		
    		if(currentPlayer == player1){
    			currentPlayer = player2;
    		}
    		
    		return currentPlayer;
    	}
    	
    }
    Last edited by _inase; 09-12-2011 at 06:11 AM.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,873
    Rep Power
    25

    Default Re: infamous tictactoe program...

    I suppose it's not checking for a winner correctly
    Sounds like time for some debugging. Add some printlns to your code to show the values of the variables that are being used to determine a winner. print them every time they are changed. If you know when there is a winner,then the print outs should show you when your logic is missing a winner so you can fix it.

    how do I get the display board to display the 'X' or 'O' after each input?
    Copy and paste there what your program displays for the board.

Similar Threads

  1. TicTacToe Help
    By Ryan10 in forum New To Java
    Replies: 16
    Last Post: 03-31-2011, 08:01 PM
  2. Help with TicTacToe
    By jokerboy123 in forum New To Java
    Replies: 1
    Last Post: 01-22-2011, 02:29 AM
  3. Help with tictactoe
    By ximenaacucr in forum New To Java
    Replies: 3
    Last Post: 09-16-2009, 09:30 PM
  4. TicTacToe
    By Thomas Covington in forum New To Java
    Replies: 1
    Last Post: 04-10-2008, 05:51 AM
  5. TicTacToe
    By Joe3161 in forum New To Java
    Replies: 4
    Last Post: 11-25-2007, 07:47 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •