Advertisement
Guest User

Untitled

a guest
Nov 27th, 2015
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.14 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.Scanner;
  3.  
  4. /**
  5. * Created by rayanelhajj98 on 25/11/2015.
  6. */
  7. public class TicTacToe {
  8. public static char[][] playingBoard = new char[3][3];
  9. public static char player1 = 'x';
  10. public static char player2 = 'o';
  11. public static int turnCounter = 0;
  12. public static boolean playing = true;
  13.  
  14. public static void main(String[] args) { //changed get winner method to return void, and in the method it prints out the winner. it activates if playing is false which is done by mechanics method.
  15. if (playing == true){play(playingBoard);}
  16.  
  17. }
  18.  
  19. //print out the playing board ;
  20. public static void printBoard(char [][] playingBoard) {
  21. for (int i = 0; i < 3; i++) {
  22. for (int j = 0; j < 3; j++) {
  23. System.out.print("[" + playingBoard[i][j] + "]");
  24. }
  25. System.out.println();
  26. }
  27. }
  28.  
  29. /**
  30. * takes inputs from player 1 or 2 ;
  31. */
  32. public static void inputReciever(char [][] playingBoard) {
  33. Scanner scan = new Scanner(System.in);
  34. System.out.println("please enter the column number : ");
  35. int Cmove = scan.nextInt();
  36. System.out.println("please enter the row" +
  37. " number : ");
  38. int Rmove = scan.nextInt();
  39.  
  40. // if statement to switch turn after each input;
  41. if (turnCounter % 2 == 0) {
  42. playingBoard[Rmove - 1][Cmove - 1] = player1;
  43. } else {
  44. playingBoard[Rmove - 1][Cmove - 1] = player2;
  45. }
  46. turnCounter++;
  47. }
  48.  
  49. /**
  50. * as long as there is no winner yet it keeps the game playing
  51. * @param playingBoard
  52. */
  53. public static void play(char [][] playingBoard) {
  54. while (playing == true) {
  55. printBoard(playingBoard);
  56. if (turnCounter % 2 == 0) { // this print out whose turn it is. you can make this into a method to make it look better
  57. System.out.println("Player" + player1 + "'s turn:");
  58. } else {
  59. System.out.println("Player " + player2 + "'s turn:");
  60. }
  61. inputReciever(playingBoard);
  62. mechanics(playingBoard);
  63. if(playing == false){
  64. playAgain();
  65. play(playingBoard);
  66. }
  67. }
  68. }
  69.  
  70. /**
  71. * after the game is finished or one of the players won,it starts a new game;
  72. */
  73. public static void playAgain(){
  74. System.out.println("if you want to play enter y if not enter n:");
  75. Scanner scan = new Scanner(System.in);
  76. String input = scan.nextLine().toUpperCase();
  77. if(input.equals("Y")){
  78. playing = true;
  79. for(int i= 0; i<3; i++){
  80. for(int j=0; j<3 ; j++){
  81. playingBoard[i][j] = ' ';
  82. }
  83. }
  84. playing = true;
  85. play(playingBoard);
  86. }
  87. else if(input.equals("N")){
  88. System.out.println("thank's for playing ! Hope to see you again. ");
  89. playing= false;
  90. }
  91. else if (!input.equals("Y") && !input.equals("N")){
  92. playing= false;
  93. }
  94. }
  95.  
  96. /**
  97. * checks if we have a winner;
  98. * @param playingBoard
  99. */
  100. public static void mechanics(char[][] playingBoard) {
  101.  
  102. for (int i = 0; i < 3; i++) {
  103. if (playingBoard[i][0] == playingBoard[i][1] && playingBoard[i][1] == playingBoard[i][2] && playingBoard[i][0] == 'x') {
  104. System.out.println("player 1 wins");
  105. playing = false;
  106. } else if (playingBoard[0][i] == playingBoard[1][i] && playingBoard[1][i] == playingBoard[2][i] && playingBoard[0][i] == 'x') {
  107. System.out.println("player 1 wins");
  108. playing = false;
  109. } else if (playingBoard[0][i] == playingBoard[1][i] && playingBoard[1][i] == playingBoard[2][i] && playingBoard[0][i] == 'o') {
  110. System.out.println("player 2 wins");
  111. playing = false;
  112. } else if (playingBoard[i][0] == playingBoard[i][1] && playingBoard[i][1] == playingBoard[i][2] && playingBoard[i][0] == 'o') {
  113. System.out.println("player 2 wins");
  114. playing = false;
  115. }
  116. }
  117. if(playingBoard[0][0] == 'x' && playingBoard[0][0]== playingBoard[1][1] && playingBoard[0][0]== playingBoard[2][2] ){
  118. System.out.println("player 1 wins");
  119. playing = false;
  120. }
  121. else if(playingBoard[0][0]=='o' && playingBoard[0][0]== playingBoard[1][1] && playingBoard[0][0]== playingBoard[2][2]){
  122. System.out.println("player 2 wins");
  123. playing = false;
  124. } else if ( playingBoard[0][2]== playingBoard[1][1] && playingBoard[1][1]== playingBoard[2][0] && playingBoard[0][2] == 'x') {
  125. System.out.println("player 1 wins");
  126. playing = false;
  127. } else if (playingBoard[0][2]== playingBoard[1][1] && playingBoard[1][1]== playingBoard[2][0] && playingBoard[0][2] == 'o') {
  128. System.out.println("player 2 wins");
  129. playing = false;
  130. }
  131. }
  132. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement