Advertisement
Guest User

Untitled

a guest
Jul 27th, 2017
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.70 KB | None | 0 0
  1. import java.util.Scanner;
  2. public class Player {
  3.  
  4. private String name; //each player's unique(?) name.
  5. private String piece; //either an x or an o -- x form p1, o for p2
  6.  
  7. //constructor, takes in a name and player number.
  8. public Player(String name, int playNum) {
  9. this.name = name;
  10. if(playNum==1)
  11. piece="x";
  12. else
  13. piece="o";
  14. }
  15. //because you just never know.
  16. public String toString(){
  17. return name;
  18. }
  19.  
  20. /*does a majority of the work for class player... prompts player for move position, checks for
  21. * invalid input (e.g. the letter k). then sends in that value to the moves[][] in class board.
  22. * checks the value in moves[][] and if it is full, restarts the turn. */
  23.  
  24. public void makeMove() {
  25. Scanner scan = new Scanner(System.in);
  26. System.out.println("\n" + name + ", it is your turn.");
  27. int col=3, row=3;
  28. boolean failing = false;
  29. do {
  30. do { //continues to loop until a valid row number is given.
  31. failing=false;
  32. System.out.println("Which column would you like your piece to go in?");
  33. try {
  34. col = scan.nextInt();
  35. } catch (Exception e) { //should only work if any number that is not an integer
  36. System.out.println("That is not a valid column number.");
  37. failing = true;
  38. } finally {
  39. scan.nextLine();
  40. }
  41. if (col > 2 || col < 0) { //starts the loop over if the row given is illegal
  42. System.out.println("The only columns are 0, 1, and 2.");
  43. failing = true;
  44. }
  45. } while(failing);
  46. do { //this loop is the same as the row loop, but uses columns instead.
  47. failing=false;
  48. System.out.println("Which row would you like your piece to go in?");
  49. try {
  50. row = scan.nextInt();
  51. } catch (Exception e) {
  52. System.out.println("That is not a valid row number.");
  53. failing = true;
  54. } finally {
  55. scan.nextLine();
  56. }
  57. if (row > 2 || row < 0) {
  58. System.out.println("The only rows are 0, 1, and 2.");
  59. failing = true;
  60. }
  61. } while(failing);
  62. if(board.getMoves(col, row).trim().isEmpty()) {
  63. board.playerChoice(col, row, piece);
  64. failing=false;
  65. }
  66. else {
  67. failing=true;
  68. System.out.println("That space is already full. Please choose again.");
  69. }
  70. } while(failing);
  71. }
  72. }
  73.  
  74.  
  75.  
  76.  
  77.  
  78.  
  79.  
  80.  
  81.  
  82.  
  83.  
  84.  
  85.  
  86. import java.util.Scanner;
  87. public class board { //-----------------------------------
  88. private static String[][] moves; //the array that holds the values and status of each place on the board
  89. private Player playerOne, playerTwo; //stores the name of each respective player
  90. private static int whoWon; //stores the three values below of either p1 winning, p2 winning, or a tie
  91. private static final int PLAYERONE = 1; //-----------------------------------
  92. private static final int PLAYERTWO = 2;
  93. private static final int ITS_A_TIE = 3;
  94.  
  95.  
  96. public static void main (String[] args) {
  97. board newGame = new board();
  98. newGame.game();
  99. }
  100.  
  101. //game() calls mostly everything relevant to the operations of ttt.
  102. public void game() {
  103. System.out.println("Welcome to Tic-Tac-Toe!");
  104. whoWon = 0; //initializes it so that no one is winning
  105. Scanner scan = new Scanner(System.in);
  106. System.out.println("Player one, what is your name?");
  107. playerOne = new Player(scan.nextLine(), 1);
  108. System.out.println("Player two, what is your name?");
  109. playerTwo = new Player(scan.nextLine(), 2);
  110. moves = new String[3][3]; //initializes the board....
  111. for(int i = 0; i < moves.length; i++) {
  112. for(int j = 0; j <moves[i].length; j++) {
  113. moves[i][j] = " "; //..and fills it with spaces to keep formatting
  114. }
  115. }
  116. do{ //this loop cycles through each players' turn.
  117. display(); //display() shows the board.
  118. playerOne.makeMove(); //explained in Player class.
  119. detectGameOver(); //at the end of each players' turn, checks for a win or draw
  120. if(whoWon < 1) { //these lines only operate if the game did not end on p1's turn
  121. display();
  122. playerTwo.makeMove();
  123. detectGameOver();
  124. }
  125. } while(whoWon < 1); //whoWon is only above 0 if someone one. 1 represents p1 winning, 2 represents p2, and 3 represents a tie. ergo, when it as at 0, there is no win result
  126. display(); //shows the board one last time with the winning move.
  127. System.out.print("\nThe game is over, and the winner is ");
  128. if (whoWon == PLAYERONE) System.out.println(playerOne + ". Congratulations and good game!");
  129. else if (whoWon == PLAYERTWO) System.out.println(playerTwo + ". Congratulations and good game!");
  130. else if (whoWon == ITS_A_TIE) System.out.println("... no one. It appears to be a tie. Very drab. Oh well, nice try.");
  131. }
  132.  
  133. //called from the player class to show their move on the board
  134. public static void playerChoice(int col, int row, String piece) {
  135. moves[col][row] = piece;
  136. }
  137.  
  138. //shows the board each turn, separated by --- and # on cross-bars
  139. public void display() {
  140. for(int i = 0; i < moves.length; i++) {
  141. for(int j = 0; j <moves[i].length; j++) {
  142. if(j < 9)
  143. System.out.print(moves[i][j] + " | ");
  144. else
  145. System.out.print(moves[i][j]);
  146. }
  147. if(i < 9)
  148. System.out.println("\n--#---#---#---#---#---#---#---#---#---");
  149. }
  150. }
  151.  
  152. //called from player
  153. public static String getMoves(int col, int row) {
  154. return moves[col][row];
  155. }
  156.  
  157. //detects the barf that is the logic. Each of the || conditions represent a possible win.
  158. public static void detectGameOver() {
  159. if((moves[0][0].equals("o") && moves[0][1].equals("o") && moves[0][2].equals("o"))
  160. || (moves[0][0].equals("o") && moves[1][1].equals("o") && moves[2][2].equals("o"))
  161. || (moves[0][0].equals("o") && moves[1][0].equals("o") && moves[2][0].equals("o"))
  162. || (moves[0][2].equals("o") && moves[1][1].equals("o") && moves[2][0].equals("o"))
  163. || (moves[2][0].equals("o") && moves[2][1].equals("o") && moves[2][2].equals("o"))
  164. || (moves[0][1].equals("o") && moves[1][1].equals("o") && moves[2][1].equals("o"))
  165. || (moves[1][0].equals("o") && moves[1][1].equals("o") && moves[1][2].equals("o"))
  166. || (moves[0][2].equals("o") && moves[1][2].equals("o") && moves[2][2].equals("o"))
  167. ){
  168. whoWon = PLAYERTWO;
  169. }
  170. else if(
  171. (moves[0][0].equals("x") && moves[0][1].equals("x") && moves[0][2].equals("x"))
  172. || (moves[0][0].equals("x") && moves[1][1].equals("x") && moves[2][2].equals("x"))
  173. || (moves[0][0].equals("x") && moves[1][0].equals("x") && moves[2][0].equals("x"))
  174. || (moves[0][2].equals("x") && moves[1][1].equals("x") && moves[2][0].equals("x"))
  175. || (moves[2][0].equals("x") && moves[2][1].equals("x") && moves[2][2].equals("x"))
  176. || (moves[0][1].equals("x") && moves[1][1].equals("x") && moves[2][1].equals("x"))
  177. || (moves[1][0].equals("x") && moves[1][1].equals("x") && moves[1][2].equals("x"))
  178. || (moves[0][2].equals("x") && moves[1][2].equals("x") && moves[2][2].equals("x"))
  179. ){
  180. whoWon = PLAYERONE;
  181. }
  182. //represents a tie-- checks if every position is full but isn't a space (which is the initialized value
  183. else if(
  184. !moves[0][0].equals(" ") && !moves[0][1].equals(" ") && !moves[0][2].equals(" ")
  185. && !moves[0][0].equals(" ") && !moves[1][1].equals(" ") && !moves[2][2].equals(" ")
  186. && !moves[0][0].equals(" ") && !moves[1][0].equals(" ") && !moves[2][0].equals(" ")
  187. && !moves[0][2].equals(" ") && !moves[1][1].equals(" ") && !moves[2][0].equals(" ")
  188. && !moves[2][0].equals(" ") && !moves[2][1].equals(" ") && !moves[2][2].equals(" ")
  189. && !moves[0][1].equals(" ") && !moves[1][1].equals(" ") && !moves[2][1].equals(" ")
  190. && !moves[1][0].equals(" ") && !moves[1][1].equals(" ") && !moves[1][2].equals(" ")
  191. && !moves[0][2].equals(" ") && !moves[1][2].equals(" ") && !moves[2][2].equals(" ")
  192. ) {
  193. whoWon = ITS_A_TIE;
  194. }
  195. }
  196. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement