Advertisement
Guest User

Untitled

a guest
Apr 19th, 2018
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.36 KB | None | 0 0
  1. package inf101.v18.sem2;
  2.  
  3. import java.awt.BorderLayout;
  4. import java.awt.Color;
  5. import java.awt.Dimension;
  6. import java.awt.Graphics;
  7. import java.awt.Graphics2D;
  8. import java.awt.GridLayout;
  9. import java.awt.RenderingHints;
  10. import java.awt.event.MouseAdapter;
  11. import java.awt.event.MouseEvent;
  12. import java.util.Scanner;
  13. import javax.swing.*;
  14.  
  15. public class Game extends JFrame {
  16.  
  17. // ------------------------------------------------------------
  18. // Name-constants to represent the seeds and cell contents
  19. public static final int EMPTY = 0;
  20. public static final int PLAYER1 = 1;
  21. public static final int PLAYER2 = 2;
  22.  
  23. // Name-constants to represent the various states of the game
  24. public static final int PLAYING = 0;
  25.  
  26. // The game board and the game status
  27. public int currentState = 1;
  28. public int currentPlayer;
  29. public int rows = 6, columns = 7, AmountToWin = 4;
  30. public int[][] board = new int[rows][columns];
  31. public Scanner input = new Scanner(System.in); // the input Scanner
  32. MouseAdapter mouseadapter;
  33. gamePanel[][] gamepanel;
  34.  
  35. // ------------------------------------------------------------
  36. public static void main(String[] args) {
  37. new Game();
  38. }
  39.  
  40. // ------------------------------------------------------------
  41. /* contructor */
  42. public Game() {
  43. initGame();
  44. gamepanel = new gamePanel[rows][columns];
  45. mouseadapter = new MouseAdapter() {
  46. @Override
  47. public void mouseClicked(MouseEvent me) {
  48. gamePanel panel = (gamePanel) me.getSource();
  49. int y = panel.column;
  50. int x = availableRow(y);
  51. if (x == -1)
  52. return;
  53.  
  54. board[x][y] = currentPlayer;
  55. if (currentPlayer == PLAYER1)
  56. currentPlayer = PLAYER2;
  57. else
  58. currentPlayer = PLAYER1;
  59. gamepanel[x][y].repaint();
  60.  
  61. if (checkForResult(currentPlayer) == 1) {
  62. showResult(currentPlayer);
  63. } else if (checkForResult(currentPlayer) == 2) {
  64. showResult(currentPlayer);
  65. }
  66. }
  67. };
  68. JPanel p1 = new JPanel();
  69. p1.setLayout(new GridLayout(rows, columns));
  70.  
  71. for (int i = 0; i < rows; i++) {
  72. for (int j = 0; j < columns; j++) {
  73. gamepanel[i][j] = new gamePanel(i, j);
  74. p1.add(gamepanel[i][j]);
  75. gamepanel[i][j].addMouseListener(mouseadapter);
  76. }
  77. }
  78. add(p1, BorderLayout.CENTER);
  79.  
  80. this.pack();
  81. this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  82. this.setLocationRelativeTo(null);
  83. this.setVisible(true);
  84.  
  85. }
  86.  
  87. // ------------------------------------------------------------
  88. public void initGame() {
  89. for (int row = 0; row < rows; ++row) {
  90. for (int col = 0; col < columns; ++col) {
  91. board[row][col] = EMPTY; // all cells empty
  92. }
  93. }
  94. currentState = PLAYING; // ready to play
  95. currentPlayer = PLAYER1; // player 1 plays first
  96. }
  97.  
  98. // ------------------------------------------------------------
  99. private int checkForResult(int player) {
  100. final int EMPTY_SLOT = 0;
  101. for (int r = 0; r < rows; r++) { // iterate rows, bottom to top
  102. for (int c = 0; c < columns; c++) { // iterate columns, left to right
  103. player = board[r][c];
  104. if (player == EMPTY_SLOT)
  105. continue; // don't check empty slots
  106.  
  107. if (c + 3 < columns && player == board[r][c + 1] && // look right
  108. player == board[r][c + 2] && player == board[r][c + 3])
  109. return player;
  110. if (r + 3 < rows) {
  111. if (player == board[r + 1][c] && // look up
  112. player == board[r + 2][c] && player == board[r + 3][c])
  113. return player;
  114. if (c + 3 < columns && player == board[r + 1][c + 1] && // look up & right
  115. player == board[r + 2][c + 2] && player == board[r + 3][c + 3])
  116. return player;
  117. if (c - 3 >= 0 && player == board[r + 1][c - 1] && // look up & left
  118. player == board[r + 2][c - 2] && player == board[r + 3][c - 3])
  119. return player;
  120. }
  121. }
  122. }
  123. return EMPTY_SLOT; // no winner found
  124. }
  125.  
  126. // -------------------------------------------
  127. // Shows the result of the winning game on a pop-out window
  128. public void showResult(int winnerPlayer) {
  129.  
  130. JFrame frameShowResult = new JFrame();
  131. if (winnerPlayer == 1) {
  132. JOptionPane.showMessageDialog(frameShowResult, "\nWinner : YELLOW", "End Game",
  133. JOptionPane.INFORMATION_MESSAGE);
  134. } else {
  135. JOptionPane.showMessageDialog(frameShowResult, "\nWinner : RED", "End Game",
  136. JOptionPane.INFORMATION_MESSAGE);
  137. }
  138. }
  139.  
  140. // ------------------------------------------------------------
  141. /** finds the first empty space in a column starting at the bottom. */
  142. public int availableRow(int col) {
  143. for (int row = rows - 1; row >= 0; row--) {
  144. if (board[row][col] == EMPTY) {
  145. return row;
  146. }
  147. }
  148. return -1;
  149. }
  150.  
  151. // ------------------------------------------------------------
  152. class gamePanel extends JPanel {
  153. int row, column;
  154.  
  155. public gamePanel(int r, int c) {
  156. row = r;
  157. column = c;
  158. this.setBackground(Color.BLUE);
  159. this.setPreferredSize(new Dimension(100, 100)); // try commenting out this line!
  160. }
  161.  
  162. // ------------------------------------------------------------
  163. public void paintComponent(Graphics g) {
  164. super.paintComponent(g);
  165. Color c = board[row][column] == EMPTY ? Color.WHITE
  166. : board[row][column] == PLAYER1 ? Color.RED : Color.YELLOW;
  167. g.setColor(c);
  168. Graphics2D g2d = (Graphics2D) g.create();
  169. g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
  170. g2d.fillOval(10, 10, 90, 90);
  171. g2d.dispose();
  172. }
  173. }
  174.  
  175. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement