Advertisement
Guest User

Untitled

a guest
Apr 22nd, 2019
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.12 KB | None | 0 0
  1. /*
  2. * To change this license header, choose License Headers in Project Properties.
  3. * To change this template file, choose Tools | Templates
  4. * and open the template in the editor.
  5. */
  6. package tictactoe;
  7.  
  8. /**
  9. *
  10. * @author DD
  11. */
  12. public class MyGame
  13. {
  14. private String [][] gameBoard = {{"1","|","2","|","3"},
  15. {"-","+","-","+","-"},{"4","|","5","|","6"},
  16. {"-","+","-","+","-"},{"7","|","8","|","9"}};
  17. private String playerGiven;
  18. private boolean victory = false;
  19.  
  20. //-------- method used to retrieve victory condition ----------------------
  21. public boolean getVictory()
  22. {
  23. return victory;
  24. }// -------------------------- End of getter ----------------------------
  25.  
  26. public void setGameBoard() {
  27. this.gameBoard = gameBoard;
  28. }
  29.  
  30.  
  31. public MyGame ()
  32. {
  33. for(int row=0;row<gameBoard.length;row++)
  34. {
  35. for(int col=0;col<gameBoard[row].length;col++)
  36. {
  37. System.out.print(gameBoard[row][col]);
  38. }
  39. System.out.println("");
  40. }
  41. System.out.println("");
  42. }// -------------- End of Constructor -------------------------
  43.  
  44. public void displayGame(String playerGiven)
  45. {
  46. for(int row=0;row<gameBoard.length;row++)
  47. {
  48. for(int col=0;col<gameBoard[row].length;col++)
  49. {
  50. System.out.print(gameBoard[row][col]);
  51. }
  52. System.out.println("");
  53. }
  54. System.out.println("It's your turn Player "+ playerGiven);
  55. }// ------------------------ End of displayGame method ----------------------
  56.  
  57. public void playingGame (String playerChoiceGiven, String playerGiven)
  58. {
  59. for(int row=0;row<5;row++)
  60. {
  61. for(int col=0;col<5;col++)
  62. {
  63. if(gameBoard[row][col].contains(playerChoiceGiven))
  64. {
  65. gameBoard[row][col]= playerGiven;
  66. }
  67. }
  68. }
  69. }// ---------------- End of playingGame method ------------------------------
  70.  
  71. public void victoryCondition()
  72. {
  73. /* check from the the -individual columns-
  74. tried making it with for loops but the compiler starting going crazy.
  75. */
  76. if ((gameBoard[0][0]==gameBoard[2][0])&&
  77. (gameBoard[2][0] == gameBoard[4][0]))
  78. {
  79. System.out.println("Player: "+gameBoard[2][0]+ " WON!");
  80. victory = true;
  81. }
  82.  
  83. if ((gameBoard[0][2]==gameBoard[2][2])&&
  84. (gameBoard[2][2] == gameBoard[4][2]))
  85. {
  86. System.out.println("Player: "+gameBoard[2][2]+ " WON!");
  87. victory = true;
  88. }
  89. if ((gameBoard[0][4]==gameBoard[2][4])&&
  90. (gameBoard[2][4] == gameBoard[4][4]))
  91. {
  92. System.out.println("Player: "+gameBoard[2][4]+ " WON!");
  93. victory = true;
  94. }
  95. /*
  96. check indivual rows are similar
  97. */
  98. if ((gameBoard[0][0]==gameBoard[0][2])&&
  99. (gameBoard[0][2] == gameBoard[0][4]))
  100. {
  101. System.out.println("Player: "+gameBoard[0][2]+ " WON!");
  102. victory = true;
  103. }
  104. if ((gameBoard[2][0]==gameBoard[2][2])&&
  105. (gameBoard[2][2] == gameBoard[2][4]))
  106. {
  107. System.out.println("Player: "+gameBoard[2][2]+ " WON!");
  108. victory = true;
  109. }
  110. if ((gameBoard[4][0]==gameBoard[4][2])&&
  111. (gameBoard[4][2] == gameBoard[4][4]))
  112. {
  113. System.out.println("Player: "+gameBoard[4][2]+ " WON!");
  114. victory = true;
  115. }
  116. /*
  117. check through the two diag patterns for winner.
  118. */
  119. if ((gameBoard[0][0]==gameBoard[2][2])&&
  120. (gameBoard[2][2] == gameBoard[4][4]))
  121. {
  122. System.out.println("Player: "+gameBoard[2][2]+ " WON!");
  123. victory = true;
  124. }
  125. if ((gameBoard[0][4]==gameBoard[2][2])&&
  126. (gameBoard[2][2] == gameBoard[4][0]))
  127. {
  128. System.out.println("Player: "+gameBoard[2][2]+ " WON!");
  129. victory = true;
  130. }
  131. // otherwise return false to continue game
  132. }
  133. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement