Advertisement
Guest User

Diagonals Working GameState

a guest
May 19th, 2019
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 16.48 KB | None | 0 0
  1. public class GameState {
  2. //double check if static is correct. I think it is.
  3.  
  4. private boolean turn; //true -> player1Turn false->player2Turn
  5. private int[][] currentBoard = new int[8][8];
  6.  
  7.  
  8. //Constructor
  9. GameState() {
  10.  
  11. //init all to 0
  12. for (int a = 0; a < 8; a++) {
  13. for (int b = 0; b < 8; b++) {
  14. currentBoard[a][b] = 0;
  15. }
  16. }
  17. //Initialize the states of the beginning pieces (pieces in the middle)
  18. for (int i = 3; i < 5; i++) {
  19. for (int a = 3; a < 5; a++) {
  20. if (i == a) {
  21. currentBoard[i][a] = 1;
  22. } else {
  23. currentBoard[i][a] = 2;
  24. }
  25. }
  26. }
  27.  
  28. }
  29.  
  30. public void setTurn(boolean turnSet) {
  31. turn = turnSet;
  32. }
  33.  
  34. public boolean getTurn() {
  35. return turn;
  36. }
  37.  
  38. public int[][] getCurrentBoard() {
  39. return currentBoard;
  40. }
  41.  
  42. public void updateCellArray(int row, int col, int state) {
  43.  
  44. if (state == 0) {
  45. currentBoard[row][col] = 0;
  46. } else if (state == 1) {
  47. currentBoard[row][col] = 1;
  48. } else if (state == 2) {
  49.  
  50. currentBoard[row][col] = 2;
  51. }
  52.  
  53.  
  54. }
  55.  
  56. public int playerTurn(boolean turn) {
  57. if (turn) {
  58. return 1; //return white int
  59. } else {
  60. return 2;
  61. }
  62. }
  63.  
  64. // Should create an array for this that has a true and false for each player. This should be only a couple lines long
  65. private boolean downDirection(int rw, int cl) {
  66. int x = rw;
  67. int y = cl;
  68.  
  69. int playerTurn = playerTurn(turn);
  70. int opponentPlayer = playerTurn(!turn);
  71.  
  72. if (currentBoard[x][y] != 0) { // NEVER USE ANYTHING BESIDES CURRENTBOARD TO CHECK THE STATE OF THE BOARD!!!
  73. return false;
  74. }
  75.  
  76. //down
  77. if (x + 1 > 7) {
  78. return false;
  79. }
  80. if (currentBoard[x + 1][y] == opponentPlayer) { // if the piece below it is the opposing player's
  81.  
  82. int num = 0;
  83.  
  84. for (int a = 2; x + a < 8; a++) {//look at the pieces below that (that's why a = 2, cause we already looked at 1)
  85.  
  86. if (a == 7) {
  87. return false;
  88. }
  89.  
  90.  
  91. if (currentBoard[x + a][y] == playerTurn) {
  92.  
  93. num = (x + a);
  94. a = 8; // breaks outta loop bc of this
  95. }
  96.  
  97. }
  98. int diff = num -x; // Difference between the expected cell's y-value and the player's next vertical piece
  99. // ..next vertical piece below it...
  100. for (int b = 0; b < diff; b++) {
  101.  
  102. System.out.println((x + b) + ", " + (y) + " Set to: " + playerTurn + " - downDirection");
  103.  
  104. updateCellArray(x + b, y, playerTurn);// Sets everything in between equal
  105.  
  106. }
  107. System.out.println("After updateCellArray in downDirection(2,2): " + currentBoard[2][2] );
  108. if(num > 1){
  109. return true;
  110. }else{
  111. return false;
  112. }
  113. } else {
  114.  
  115. return false;
  116. }
  117. }
  118.  
  119. private boolean upDirection(int rw, int cl) {
  120. int x = rw;
  121. int y = cl;
  122.  
  123. int playerTurn = playerTurn(turn);
  124. int opponentPlayer = playerTurn(!turn);
  125.  
  126. if (currentBoard[x][y] != 0) { // NEVER USE ANYTHING BESIDES CURRENTBOARD TO CHECK THE STATE OF THE BOARD!!!
  127. return false;
  128. }
  129.  
  130. //checks up
  131. if (x - 1 < 0) {
  132. return false;
  133. }
  134. if (currentBoard[x - 1][y] == opponentPlayer) { // if the piece below it is the opposing player's
  135.  
  136. int num = 0;
  137.  
  138. for (int a = 2; x - a > 0; a++) {//look at the pieces below that (that's why a = 2, cause we already looked at 1)
  139.  
  140. if (a == 7) {
  141. return false;
  142. }
  143. if (currentBoard[x - a][y] == playerTurn) {
  144.  
  145. num = (y + a);
  146. a = 8; // breaks outta loop bc of this
  147. }
  148.  
  149. }
  150. int diff = num - y; // Difference between the expected cell's y-value and the player's next vertical piece
  151. // ..next vertical piece below it...
  152. for (int b = 0; b < diff; b++) {
  153.  
  154. System.out.println((x - b) + ", " + (y) + " Set to: " + playerTurn + " - upDirection");
  155. updateCellArray(x - b, y, playerTurn);// Sets everything in between equal
  156. }
  157. if(num > 1){
  158. return true;
  159. }else{
  160. return false;
  161. }
  162. } else {
  163.  
  164. return false;
  165. }
  166. }
  167.  
  168. private boolean leftDirection(int rw, int cl) {
  169. int x = rw;
  170. int y = cl;
  171.  
  172. int playerTurn = playerTurn(turn);
  173. int opponentPlayer = playerTurn(!turn);
  174.  
  175. if (currentBoard[x][y] != 0) {
  176. return false;
  177. }
  178.  
  179. //left
  180. if (y + 1 > 7) {
  181. return false;
  182. }
  183. if (currentBoard[x][y + 1] == opponentPlayer) { // if the piece right of it it is the opposing player's
  184.  
  185. int num = 0;
  186.  
  187. for (int a = 2; x + a < 8; a++) {//look at the pieces below that (that's why a = 2, cause we already looked at 1)
  188.  
  189. if (a == 7) {
  190. return false;
  191. }
  192. if (currentBoard[x][y + a] == playerTurn) {
  193.  
  194. num = (y + a);
  195. a = 8; // breaks outta loop bc of this
  196. }
  197.  
  198. }
  199. int diff = num - y; // Difference between the expected cell's y-value and the player's next vertical piece
  200. // ..next vertical piece below it...
  201. for (int b = 0; b < diff; b++) {
  202.  
  203. System.out.println(x + ", " + (y + b) + " Set to: " + playerTurn + " - leftDirection");
  204. updateCellArray(x, y + b, playerTurn);// Sets everything in between equal
  205. }
  206. if(num > 1){
  207. return true;
  208. }else{
  209. return false;
  210. }
  211. } else {
  212.  
  213. return false;
  214. }
  215. }
  216.  
  217. private boolean rightDirection(int rw, int cl) {
  218. int x = rw;
  219. int y = cl;
  220.  
  221. int playerTurn = playerTurn(turn);
  222. int opponentPlayer = playerTurn(!turn);
  223.  
  224. if (currentBoard[x][y] != 0) { // NEVER USE ANYTHING BESIDES CURRENTBOARD TO CHECK THE STATE OF THE BOARD!!!
  225. return false;
  226. }
  227. if (y - 1 < 0) {
  228. return false;
  229. }
  230. //right
  231. if (currentBoard[x][y - 1] == opponentPlayer) { // if the piece below it is the opposing player's
  232. System.out.println("Recognizes that the piece left of it is the Opposing Player (in rightDirection Method)");
  233. int num = 0;
  234.  
  235. for (int a = 2; x + a < 8; a++) {//look at the pieces below that (that's why a = 2, cause we already looked at 1)
  236.  
  237. if (a == 7) {
  238. return false;
  239. }
  240. if (currentBoard[x][y - a] == playerTurn) {
  241.  
  242. num = (y + a );
  243. System.out.println("currentBoard: " + x + " "+ (y-a) + "State:" + currentBoard[x][y-a]);
  244. a = 8; // breaks outta loop bc of this
  245. }
  246.  
  247. }
  248. int diff = num - y; // Difference between the expected cell's y-value and the player's next vertical piece
  249. // ..next vertical piece below it...
  250. for (int b = 0; b < diff; b++) {
  251.  
  252. System.out.println(x + ", " + (y - b) + " Set to: " + playerTurn + " - rightDirection");
  253. updateCellArray(x, y - b, playerTurn);// Sets everything in between equal
  254. }
  255. if(num > 1){
  256. return true;
  257. }else{
  258. return false;
  259. }
  260. } else {
  261.  
  262. return false;
  263. }
  264.  
  265.  
  266. }
  267.  
  268. private boolean downLeftDirection(int rw, int cl) {
  269. int x = rw;
  270. int y = cl;
  271.  
  272. int playerTurn = playerTurn(turn);
  273. int opponentPlayer = playerTurn(!turn);
  274.  
  275. if (currentBoard[x][y] != 0) { // NEVER USE ANYTHING BESIDES CURRENTBOARD TO CHECK THE STATE OF THE BOARD!!!
  276. return false;
  277. }
  278.  
  279. //down
  280. if (x + 1 > 7 || y + 1 > 7 ) {
  281.  
  282. return false;
  283. }
  284. if (currentBoard[x + 1][y+1] == opponentPlayer) { // if the piece below it is the opposing player's
  285.  
  286. int num = 0;
  287.  
  288. for (int a = 2; x + a < 8 && y + a < 8; a++) {//look at the pieces below that (that's why a = 2, cause we already looked at 1)
  289.  
  290. if (a == 7) {
  291. return false;
  292. }
  293.  
  294.  
  295. if (currentBoard[x + a][y + a] == playerTurn) {
  296.  
  297. num = (x + a);
  298. a = 8; // breaks outta loop bc of this
  299. }
  300.  
  301. }
  302. int diff = num - x; // Difference between the expected cell's y-value and the player's next vertical piece
  303. // ..next vertical piece below it...
  304. for (int b = 0; b < diff; b++) {
  305.  
  306. updateCellArray(x + b, y + b, playerTurn);// Sets everything in between equal
  307.  
  308. }
  309.  
  310. if(num > 1){
  311. return true;
  312. }else{
  313. return false;
  314. }
  315. } else {
  316.  
  317. return false;
  318. }
  319. }
  320.  
  321. private boolean downRightDirection(int rw, int cl) {
  322. int x = rw;
  323. int y = cl;
  324.  
  325. int playerTurn = playerTurn(turn);
  326. int opponentPlayer = playerTurn(!turn);
  327.  
  328. if (currentBoard[x][y] != 0) { // NEVER USE ANYTHING BESIDES CURRENTBOARD TO CHECK THE STATE OF THE BOARD!!!
  329. return false;
  330. }
  331.  
  332. //down
  333. if (x + 1 > 7 || y - 1 < 0 ) {
  334.  
  335. return false;
  336. }
  337. if (currentBoard[x + 1][y-1] == opponentPlayer) { // if the piece below it is the opposing player's
  338.  
  339. int num = 0;
  340.  
  341. for (int a = 2; x + a < 8 && y - a > 0; a++) {//look at the pieces below that (that's why a = 2, cause we already looked at 1)
  342.  
  343. if (a == 7) {
  344. return false;
  345. }
  346.  
  347.  
  348. if (currentBoard[x + a][y - a] == playerTurn) {
  349.  
  350. num = (x + a);
  351. a = 8; // breaks outta loop bc of this
  352. }
  353.  
  354. }
  355. // THERE WILL PROBABLY BE AN ISSUE HERE WITH DIFF AND MIGHT NEED TO CREATE TWO OF THEM
  356. int diff = num - x; // Difference between the expected cell's y-value and the player's next vertical piece
  357. // ..next vertical piece below it...
  358. for (int b = 0; b < diff; b++) {
  359.  
  360. updateCellArray(x + b, y - b, playerTurn);// Sets everything in between equal
  361.  
  362. }
  363.  
  364. if(num > 1){
  365. return true;
  366. }else{
  367. return false;
  368. }
  369. } else {
  370.  
  371. return false;
  372. }
  373. }
  374.  
  375. private boolean upRightDirection(int rw, int cl) {
  376. int x = rw;
  377. int y = cl;
  378.  
  379. int playerTurn = playerTurn(turn);
  380. int opponentPlayer = playerTurn(!turn);
  381.  
  382. if (currentBoard[x][y] != 0) { // NEVER USE ANYTHING BESIDES CURRENTBOARD TO CHECK THE STATE OF THE BOARD!!!
  383. return false;
  384. }
  385.  
  386. //down
  387. if (x - 1 < 0 || y - 1 < 0 ) {
  388.  
  389. return false;
  390. }
  391. if (currentBoard[x - 1][y-1] == opponentPlayer) { // if the piece below it is the opposing player's
  392.  
  393. int num = 0;
  394.  
  395. for (int a = 2; x - a > 0 && y - a > 0; a++) {//look at the pieces below that (that's why a = 2, cause we already looked at 1)
  396.  
  397. if (a == 7) {
  398. return false;
  399. }
  400.  
  401.  
  402. if (currentBoard[x - a][y - a] == playerTurn) {
  403.  
  404. num = (x + a);
  405. a = 8; // breaks outta loop bc of this
  406. }
  407.  
  408. }
  409. // THERE WILL PROBABLY BE AN ISSUE HERE WITH DIFF AND MIGHT NEED TO CREATE TWO OF THEM
  410. int diff = num - x; // Difference between the expected cell's y-value and the player's next vertical piece
  411. // ..next vertical piece below it...
  412. for (int b = 0; b < diff; b++) {
  413.  
  414. updateCellArray(x - b, y - b, playerTurn);// Sets everything in between equal
  415.  
  416. }
  417.  
  418. if(num > 1){
  419. return true;
  420. }else{
  421. return false;
  422. }
  423. } else {
  424.  
  425. return false;
  426. }
  427. }
  428.  
  429. private boolean upLeftDirection(int rw, int cl) {
  430. int x = rw;
  431. int y = cl;
  432.  
  433. int playerTurn = playerTurn(turn);
  434. int opponentPlayer = playerTurn(!turn);
  435.  
  436. if (currentBoard[x][y] != 0) { // NEVER USE ANYTHING BESIDES CURRENTBOARD TO CHECK THE STATE OF THE BOARD!!!
  437. return false;
  438. }
  439.  
  440. //down
  441. if (x - 1 < 0 || y + 1 > 7 ) {
  442.  
  443. return false;
  444. }
  445. if (currentBoard[x - 1][y+1] == opponentPlayer) { // if the piece below it is the opposing player's
  446.  
  447. int num = 0;
  448.  
  449. for (int a = 2; x - a > 0 && y + a < 8; a++) {//look at the pieces below that (that's why a = 2, cause we already looked at 1)
  450.  
  451. if (a == 7) {
  452. return false;
  453. }
  454.  
  455.  
  456. if (currentBoard[x - a][y + a] == playerTurn) {
  457.  
  458. num = (x + a);
  459. a = 8; // breaks outta loop bc of this
  460. }
  461.  
  462. }
  463. // THERE WILL PROBABLY BE AN ISSUE HERE WITH DIFF AND MIGHT NEED TO CREATE TWO OF THEM
  464. int diff = num - x; // Difference between the expected cell's y-value and the player's next vertical piece
  465. // ..next vertical piece below it...
  466. for (int b = 0; b < diff; b++) {
  467.  
  468. updateCellArray(x - b, y + b, playerTurn);// Sets everything in between equal
  469.  
  470. }
  471.  
  472. if(num > 1){
  473. return true;
  474. }else{
  475. return false;
  476. }
  477. } else {
  478.  
  479. return false;
  480. }
  481. }
  482.  
  483. public boolean isPlaceable(int rw, int cl) {
  484. //have to implement if there is more than one as well that is true
  485.  
  486. // System.out.println("Turn in isPlaceable: " + turn);
  487. if (downLeftDirection(rw, cl)) {
  488. // System.out.println("2,2: " +currentBoard[2][2]);
  489. System.out.println("downLeftDirectionTrue");
  490.  
  491. return true;
  492. }else if (downRightDirection(rw, cl)) {
  493. System.out.println("downRightDirection true");
  494. return true;
  495. } else if (upRightDirection(rw, cl)) {
  496. System.out.println("downRightDirection true");
  497. return true;
  498. } else if (upLeftDirection(rw, cl)) {
  499. System.out.println("downRightDirection true");
  500. return true;
  501. }else if (downDirection(rw, cl)) {
  502. System.out.println("downDirection true");
  503. return true;
  504. }else if (upDirection(rw, cl)) {
  505. System.out.println("upDirection true");
  506. return true;
  507. } else if (leftDirection(rw, cl)) {
  508. System.out.println("leftDirection true");
  509. return true;
  510. } else if (rightDirection(rw, cl)) {
  511. System.out.println("rightDirection true");
  512. return true;
  513. } else {
  514. return false;
  515. }
  516.  
  517.  
  518. }
  519.  
  520. public int PlayerTwoPoints() {
  521. int points = 0;
  522. for (int a = 0; a < 8; a++) {
  523. for (int b = 0; b < 8; b++) {
  524. if (currentBoard[a][b] == 2) {
  525. points++;
  526. }
  527. }
  528. }
  529. return points;
  530. }
  531.  
  532. public int PlayerOnePoints() {
  533. int points = 0;
  534. for (int a = 0; a < 8; a++) {
  535. for (int b = 0; b < 8; b++) {
  536. if (currentBoard[a][b] == 1) {
  537. points++;
  538. }
  539. }
  540. }
  541. return points;
  542. }
  543.  
  544. //look at a column, row, or diagonal. If there is a piece that is the player's color is blocked by an opponent's color and there is no empty piece in between, placeable is true
  545.  
  546.  
  547. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement