Advertisement
Guest User

Tic tac toe code

a guest
Feb 22nd, 2022
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.68 KB | None | 0 0
  1. package JavaPackage;
  2. import java.util.*;
  3. public class TicTacToe {
  4. static boolean repeat = true;
  5. private static int turns;
  6. static Scanner input = new Scanner(System.in);
  7. public static void main(String[] args) {
  8. String[][] board = new String[3][3];
  9. for (int x = 0; x < 3; x++) {
  10. for (int y = 0; y < 3; y++) {
  11. board[x][y] = "|_|";
  12. System.out.print(board[x][y]);
  13. }
  14. System.out.println();
  15. }
  16. while (repeat) {
  17.  
  18. numpick(board);
  19. }
  20. }
  21. private static String[][] numpick(String[][] board) {
  22.  
  23.  
  24. int pinput = input.nextInt();
  25. turns++;
  26. String player="";
  27. if(turns%2==1) {
  28. player="|X|";
  29. }else {
  30. player="|O|";
  31. }
  32.  
  33. if (pinput == 7) {
  34.  
  35. board[0][0]=player;
  36.  
  37. } else if (pinput == 8) {
  38.  
  39. board[0][1]=player;
  40.  
  41. } else if (pinput == 9) {
  42.  
  43. board[0][2]=player;
  44.  
  45. } else if (pinput == 4) {
  46.  
  47. board[1][0]=player;
  48.  
  49. } else if (pinput == 5) {
  50.  
  51. board[1][1]=player;
  52.  
  53. } else if (pinput == 6) {
  54.  
  55. board[1][2]=player;
  56.  
  57. } else if (pinput == 1) {
  58.  
  59. board[2][0]=player;
  60.  
  61. } else if (pinput == 2) {
  62.  
  63. board[2][1]=player;
  64.  
  65. } else if (pinput == 3) {
  66.  
  67. board[2][2]=player;
  68.  
  69. }
  70.  
  71. for (int x = 0; x < 3; x++) {
  72. for (int y = 0; y < 3; y++) {
  73. System.out.print(board[x][y]);
  74. }
  75. System.out.println();
  76. }
  77. return board;
  78.  
  79. }
  80. }
  81.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement