-4

I have created a simple tic tac toe game in Java. The game and code works however when someone wins it lets the other player play once more before terminating and says that particular player has won. I am trying to figure out a way to allow the game to stop when a player has won. I have looked at a few different algorithms for this type of scenario here and here. But am still getting confused.

Are there any suggestions?

edit: this code is fully working now, with invalid moves not allowed anymore.

import java.util.Scanner;

public class TicTacToe{
    public static Scanner scan = new Scanner(System.in);
    private static int boardSize = 3;
    private static char[][] board = new char[boardSize][boardSize];
    private static char turn = 'O';
    private static char moveNum = 0;

public static void initiliaze(){
    for(int i = 0; i<boardSize;i++){
        for(int j = 0;j<boardSize;j++){
            board[i][j]='-';
        }
    }
}

public static void printBoard(){
    for(int i=0; i<boardSize;i++){
        for(int j=0; j<boardSize;j++){
            System.out.print(board[i][j]);
            if (j != boardSize-1){
                System.out.print("|");
            }
        }
            System.out.println();
            if(i!=boardSize-1){
                System.out.println("-----");
            }
    }
    System.out.println();
}

public static boolean checkWin(){
    if(((board[0][0]==turn && board[0][1]==turn && board[0][2]==turn)||
    (board[1][0]==turn && board[1][1]==turn && board[1][2]==turn)||
    (board[2][0]==turn && board[2][2]==turn && board[2][2]==turn)||
    (board[0][0]==turn && board[1][0]==turn && board[2][0]==turn)||
    (board[0][1]==turn && board[1][1]==turn && board[2][1]==turn)||
    (board[0][2]==turn && board[1][2]==turn && board[2][2]==turn)||
    (board[0][0]==turn && board[1][1]==turn && board[2][2]==turn)||
    (board[0][2]==turn && board[1][1]==turn && board[2][0]==turn)))
    return true;

    return false;
}

public static void makeMove(){
    int row, col;
    boolean validInput = false;

    do{
    if(turn == 'X'){
        System.out.print("Player X enter your move (row:1,2,3)!");
    }else{
        System.out.print("Player O enter your move (row:1,2,3)!");
    }
    row = scan.nextInt() - 1;

    if(turn == 'X'){
        System.out.print("Player X enter your move (col:1,2,3)!");
    }else{
        System.out.print("Player O enter your move (col:1,2,3)!");
    }
    col = scan.nextInt() - 1;
    if(row >= 0 && row < boardSize && col >=0 && col < boardSize && board[row][col] == '-'){
    board[row][col] = turn;
    validInput = true;
    }else{
         System.out.println("This move at (" + (row + 1) + "," + (col + 1)
                  + ") is not valid. Try again...");
    }
    }while(!validInput);
}

public static void changeMove(){
    if(turn == 'X'){
        turn = 'O';
    }else{
        turn = 'X';
    }
    moveNum++;
}

public static void play(){

do{
    changeMove();
    makeMove();
    printBoard();
  }while(checkWin()==false && (moveNum < boardSize*boardSize));
  if (turn=='X' && checkWin()==true){
    System.out.println("X has won");
  }
  if(checkWin() == true && turn=='O'){
    System.out.println("O has won");
  }else{
    System.out.println("This is a draw!");
  }
}


}


class PlayGame {
    public static void main (String[] args){
        TicTacToe.initiliaze();
        TicTacToe.printBoard();
        TicTacToe.play();

    }
}
Community
  • 1
  • 1
mp252
  • 453
  • 1
  • 6
  • 18

2 Answers2

1

You problem: You ask for the user to enter a move, then in the same function you change the turn to the next player. In your checkWin function, you only check if the player whose turn it is has won. By immediately changing the turn after moving, AND THEN checking for a win, the turn has switched to the other player, and it is no longer the player who just won's turn, yielding false from checkWin().

In other words, what you are doing is asking a player to move, then checking if the next player has won, then letting him move. So instead of doing a move then check for win, you are doing a check for win, then move.

Also:

  1. In your do while loop in the play() function, the checkWin(); statement all by itself does nothing and should be removed.
  2. In checkWin(), your first if condition about whether it is X's or O's turn is useless because it is always only X's turn or O's turn.
Adam Evans
  • 2,072
  • 1
  • 20
  • 29
  • thank you for the help, I understand what I was doing wrong. I have amended the changes above. Thank you again – mp252 Jun 23 '15 at 17:57
0

It is allowing user for invalid moves.

Same moves[1,1] more than once

Same move by opponent[for ex Both X and O can enter 1,2]

Invalid values like[String or values other than 1,2,3] throw Exceptions

Ram
  • 3,092
  • 10
  • 40
  • 56
Madhan
  • 5,750
  • 4
  • 28
  • 61
  • thanks I am going to start writing code for these problems now. So far I have just been doing method by method, so that I can test them individually. – mp252 Jun 23 '15 at 18:02
  • I have added in this functionality. It does not allow invalid moves anymore. – mp252 Jun 24 '15 at 12:13