Guest User

Untitled

a guest
Dec 17th, 2017
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.66 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 challenges_bboy;
  7.  
  8. import java.util.Scanner;
  9.  
  10. /**
  11. *
  12. * @author XFACON
  13. */
  14. public class XO_procedural {
  15.  
  16. static int diemention = 3;
  17. static String[][] board = new String[diemention][diemention];//กะดานเกมxo
  18. static String sign = "#";//ค่าเริ่มต้นที่จะแสดงในตาราง
  19.  
  20. static int player = 1;
  21. final static Scanner kb = new Scanner(System.in);
  22.  
  23. public static void main(String[] args) {
  24.  
  25. //init
  26. //กำหนดค่าเริ่มต้นกะดาน
  27. init();
  28. while (true) {
  29. //draw table
  30. drawBoard();
  31. //เช็คผู้เล่น เพื่อรับค่าจากผู้เล่น
  32. if (player == 1) {
  33. posit("O");
  34. } else {
  35. posit("X");
  36. }
  37. //เช็คว่าใครชนะ จะแสดงผู้ชนะ
  38. if (!board[0][0].equals(sign) && !board[0][1].equals(sign) && !board[0][2].equals(sign)) {
  39. break;
  40. } else if (!board[1][0].equals(sign) && !board[1][1].equals(sign) && !board[1][2].equals(sign)) {
  41. break;
  42. } else if (!board[2][0].equals(sign) && !board[2][1].equals(sign) && !board[2][2].equals(sign)) {
  43. break;
  44. } else if (!board[0][0].equals(sign) && !board[1][0].equals(sign) && !board[2][0].equals(sign)) {
  45. break;
  46. } else if (!board[0][1].equals(sign) && !board[1][1].equals(sign) && !board[2][1].equals(sign)) {
  47. break;
  48. } else if (!board[0][2].equals(sign) && !board[1][2].equals(sign) && !board[2][2].equals(sign)) {
  49. break;
  50. } else if (!board[0][0].equals(sign) && !board[1][1].equals(sign) && !board[2][2].equals(sign)) {
  51. break;
  52. } else if (!board[2][0].equals(sign) && !board[1][1].equals(sign) && !board[0][2].equals(sign)) {
  53. break;
  54. }
  55. }
  56. }
  57.  
  58. public static void init() {
  59. for (int row = 0; row < board.length; row++) {
  60. for (int col = 0; col < board[row].length; col++) {
  61. board[row][col] = sign;
  62. }
  63. }
  64. }
  65.  
  66. public static void drawBoard() {
  67. for (int row = 0; row < board.length; row++) {
  68. for (int col = 0; col < board[row].length; col++) {
  69. System.out.print(board[row][col]);
  70. }
  71. System.out.println("");
  72. }
  73. }
  74.  
  75. public static void posit(String signPlayer) {
  76. System.out.print("Player1 in position:");
  77. int position = kb.nextInt();
  78. setBoard(position,signPlayer);
  79. if(player==1){
  80. XO_procedural.player = 2;
  81. }
  82. else{
  83. XO_procedural.player = 1;
  84. }
  85. }
  86.  
  87. public static void setBoard(int position, String sign) {
  88. if (position == 1) {
  89. board[0][0] = sign;
  90. } else if (position == 2) {
  91. board[0][1] = sign;
  92. } else if (position == 3) {
  93. board[0][2] = sign;
  94. } else if (position == 4) {
  95. board[1][0] = sign;
  96. } else if (position == 5) {
  97. board[1][1] = sign;
  98. } else if (position == 6) {
  99. board[1][2] = sign;
  100. } else if (position == 7) {
  101. board[2][0] = sign;
  102. } else if (position == 8) {
  103. board[2][1] = sign;
  104. } else if (position == 9) {
  105. board[2][2] = sign;
  106. }
  107. }
  108. }
Add Comment
Please, Sign In to add comment