fork download
  1. /*TicTacToeTest.java*/
  2. import java.util.Scanner;
  3.  
  4. public class TicTacToeTest{
  5.  
  6. public static void main( String[] args ){
  7. boolean isFull = false;//if true all the squares have been assigned.
  8. boolean isDraw = false;//to check if the game is draw
  9. boolean squareStatus = false;//returns the status of a square, if false the square is full if true the square is empty
  10. boolean victory = false;//if returns true one of the players has won
  11. int playerCounter = 1;//if it is odd it's player1's turn if even it's player2's turn
  12. int rowInput;
  13. int colInput;
  14. Scanner input = new Scanner( System.in );
  15. TicTacToe myTicTacToe = new TicTacToe();//create object
  16. myTicTacToe.printBoard();
  17. while( !isFull ){
  18. isDraw = myTicTacToe.isDraw();
  19. if( isDraw ){
  20. System.out.println( "The game is draw!\n" );
  21. //print board
  22. //myTicTacToe.printBoard();
  23. break;
  24. }
  25. //print board
  26. //myTicTacToe.printBoard();
  27. //System.out.println("the number is " + playerCounter );
  28. if( !(myTicTacToe.isOdd( playerCounter ))){//player 1
  29. System.out.println( "\nPlayer 1: enter the coordinates of the square.\nRow and column - values between 1 and 3." );
  30. }
  31. else{//player 2
  32. System.out.println( "\nPlayer 2: enter the coordinates of the square.\nRow and column - values between 1 and 3." );
  33. }
  34. System.out.println( "\nRow: " );
  35. rowInput = input.nextInt();
  36. System.out.println( "\nColumn: " );
  37. colInput = input.nextInt();
  38. rowInput = myTicTacToe.optimizeInput( rowInput );//to subtract 1 from the input
  39. colInput = myTicTacToe.optimizeInput( colInput );//to subtract 1 from the input
  40.  
  41. //check input
  42. squareStatus = myTicTacToe.checkSquare( rowInput, colInput );
  43. //if the square is taken - true - input again row and column till you find a free one
  44. while( squareStatus ){
  45. System.out.println( "Square taken, try again. \nEnter the coordinates of the square.\nRow and column - values between 1 and 3." );
  46. System.out.println( "\nRow: " );
  47. rowInput = input.nextInt();
  48. System.out.println( "\nColumn: " );
  49. colInput = input.nextInt();
  50. rowInput = myTicTacToe.optimizeInput( rowInput );//to subtract 1 from the input
  51. colInput = myTicTacToe.optimizeInput( colInput );//to subtract 1 from the input
  52. //check input
  53. squareStatus = myTicTacToe.checkSquare( rowInput, colInput );
  54. }
  55. //assign the symbol to the chosen square
  56. myTicTacToe.assignSymbol( rowInput, colInput, playerCounter );
  57. //print board
  58. myTicTacToe.printBoard();
  59. //check for draw
  60. victory = myTicTacToe.checkVictory( rowInput, colInput, playerCounter );
  61. //player 1
  62. if(( victory ) && ( myTicTacToe.isOdd( playerCounter ) )){
  63. System.out.println( " Player 1 wins!" );
  64. //print board
  65. //myTicTacToe.printBoard();
  66. break;
  67. }
  68. //player2
  69. else if(( victory ) && !( myTicTacToe.isOdd( playerCounter ) )){
  70. System.out.println( " Player 2 wins!" );
  71. //print board
  72. myTicTacToe.printBoard();
  73. break;
  74. }
  75. playerCounter++;//increment counter to change player's turn
  76. }//end while loop
  77. }//end main
  78. }//end TicTacToeTest class
Not running #stdin #stdout 0s 0KB
stdin
Standard input is empty
stdout
Standard output is empty