Advertisement
Guest User

Haul parrington

a guest
Sep 27th, 2016
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.63 KB | None | 0 0
  1.  
  2. import java.io.*;
  3. import java.util.*;
  4. import java.util.Arrays;
  5.  
  6. public class Solution {
  7. static String[] board=new String[9];
  8. static String[] defaultBoard=new String[9];
  9. //Builds board
  10. static void Board(String[] board)
  11. {
  12. System.out.println(board[0]+"|"+board[1]+"|"+board[2]);
  13. System.out.println("_____");
  14. System.out.println(board[3]+"|"+board[4]+"|"+board[5]);
  15. System.out.println("_____");
  16. System.out.println(board[6]+"|"+board[7]+"|"+board[8]);
  17. }
  18. //Scans for number input, returns a new board state
  19. static String[] turn(Scanner scan,String[] board,String side)
  20. {
  21. String next=scan.nextLine();
  22. if(check(next)!=true)
  23. {
  24. System.out.println("You need to print an integer between 1-9.");
  25. turn(scan,board,side);
  26. }
  27. else{
  28. String hold=next;
  29. int convert=Integer.parseInt(hold);
  30. if(board[convert-1]!=" ")
  31. {
  32. System.out.println("This space is filled.");
  33. Board(board);
  34. turn(scan,board,side);
  35. }
  36. else{
  37. board[convert-1]=side;
  38. }
  39. }
  40. return board;
  41. }
  42. //Check if input is number
  43. static boolean check(String x)
  44. {
  45. if(x.matches("[1-9]"))
  46. {
  47. return true;
  48. }
  49. return false;
  50. }
  51.  
  52. //Check gamestate
  53. static int gamestate(String[] board)
  54. {
  55. String[] check=new String[8];
  56. boolean available=Arrays.asList(board).contains(" ");
  57. boolean win=false;
  58. //Check rows
  59. for(int i=0;i<3;i++)
  60. check[i]=board[i*3]+board[i*3+1]+board[i*3+2];
  61. //Check columns
  62. for(int i=0;i<3;i++)
  63. check[3+i]=board[i]+board[i+3]+board[i+6];
  64. //Check diagonals
  65. check[6]=board[0]+board[4]+board[8];
  66. check[7]=board[2]+board[4]+board[6];
  67.  
  68. if(Arrays.asList(check).contains("XXX")||Arrays.asList(check).contains("OOO"))
  69. {
  70. win=true;
  71. }
  72.  
  73. //Keep playing
  74. if(available==true&&win==false)
  75. {
  76. return 0;
  77. }
  78. //Tie
  79. else if(available==false&&win==false)
  80. {
  81. return 1;
  82. }
  83. else{
  84. //Win
  85. return -1;
  86. }
  87. }
  88.  
  89. public static void main(String[] args) {
  90. //Establish board and rules
  91. Arrays.fill(board," ");
  92. System.out.println("Instructions:");
  93. System.out.println("Here is your board.");
  94. Board(board);
  95. for(int i=1;i<=9;i++)
  96. {
  97. defaultBoard[i-1]=i+"";
  98. }
  99. System.out.println("During your turn, just place the number corresponding to the position you want to place your marker.");
  100. Board(defaultBoard);
  101.  
  102.  
  103. //Start taking inputs
  104. Scanner scan=new Scanner(System.in);
  105.  
  106. //Turns
  107. int x=1;
  108. String[] y={"X","O"};
  109. while(gamestate(board)==0)
  110. {
  111. System.out.println("Player "+x+" Turn. Place an "+y[x-1]+".");
  112. board=turn(scan,board,y[x-1]);
  113. Board(board);
  114. if(x==1)
  115. {
  116. x=2;
  117. }
  118. else
  119. {
  120. x=1;
  121. }
  122. }
  123.  
  124. if(gamestate(board)>0)
  125. {
  126. System.out.println("It's a tie.");
  127. }
  128. else{
  129. System.out.println("You win!");
  130. }
  131. scan.close();
  132. }
  133. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement