Advertisement
Crenox

Final Exam: Connect 4 GUI

May 17th, 2016
276
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.27 KB | None | 0 0
  1. HELPFUL LINKS
  2. http://www.dreamincode.net/forums/topic/234693-connect-four-gui/
  3. https://github.com/sloev/connect-4---java/blob/master/src/connect4/gui.java
  4. http://www.coderanch.com/t/642941/GUI/java/Connect-game-edited-version
  5. http://cs.gettysburg.edu/~tneller/cs111/connect4/part1/files/Connect4.java
  6. https://electronicentertainment.wordpress.com/2009/11/13/here-is-a-small-java-connect-4-game-i-created/
  7. -----------------------------------------------------------------------------------------------------------------------------------
  8. // Connect 4
  9. // Description: This class is the GUI part of the program. It is the Main Menu Section.
  10. // Sammy Samkough
  11.  
  12. import java.awt.*;
  13. import java.awt.event.*;
  14. import javax.swing.*;
  15.  
  16. public class Connect4GUI extends JFrame
  17. {
  18. // size of screen
  19. public static final int WIDTH = 400;
  20. public static final int HEIGHT = 500;
  21.  
  22. // title
  23. public static final String text = "MALLIKATRON CONNECT 4";
  24. private JLabel title;
  25.  
  26. /* Buttons Include: 1) Start 2) About
  27. Start - starts the game
  28. About - includes the game layout, how it works, and the people behind the project
  29. */
  30. private JButton startB, aboutB, exitB;
  31.  
  32. // layout
  33. private FlowLayout layout = new FlowLayout(FlowLayout.LEFT, 50, 50);
  34.  
  35. public Connect4GUI()
  36. {
  37. // this is for the space between the buttons as well
  38. this.setLayout(new FlowLayout(FlowLayout.LEFT, 50, 50));
  39.  
  40. // buttons
  41. startB = new JButton("START");
  42. startB.setPreferredSize(new Dimension(300, 50));
  43.  
  44. aboutB = new JButton("ABOUT");
  45. aboutB.setPreferredSize(new Dimension(300, 50));
  46.  
  47. exitB = new JButton("EXIT");
  48. exitB.setPreferredSize(new Dimension(300, 50));
  49. if(exitB.getModel().isPressed())
  50. {
  51. int confirm = JOptionPane.showOptionDialog(null, "Are You Sure to Close Application?", "Exit Confirmation", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, null, null);
  52.  
  53. if (confirm == 0)
  54. {
  55. System.exit(0);
  56. }
  57. }
  58.  
  59. // labels
  60. title = new JLabel(text);
  61. title.setFont(title.getFont().deriveFont(24.0f));
  62.  
  63. // additions
  64. add(title);
  65. add(startB);
  66. add(aboutB);
  67. add(exitB);
  68.  
  69. // listeners
  70. event e1 = new event();
  71. startB.addActionListener(e1);
  72.  
  73. event2 e2 = new event2();
  74. aboutB.addActionListener(e2);
  75.  
  76. event3 e3 = new event3();
  77. exitB.addActionListener(e3);
  78.  
  79. addWindowListener(exitListener);
  80. }
  81.  
  82. WindowListener exitListener = new WindowAdapter()
  83. {
  84. @Override
  85. public void windowClosing(WindowEvent e)
  86. {
  87. int confirm = JOptionPane.showOptionDialog(null, "Are you sure you want to close application?", "Exit Confirmation", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, null, null);
  88.  
  89. if (confirm == 0)
  90. {
  91. System.exit(0);
  92. }
  93. }
  94. };
  95.  
  96. public class event implements ActionListener
  97. {
  98. public void actionPerformed(ActionEvent e)
  99. {
  100. Connect4GUIStart gui = new Connect4GUIStart();
  101.  
  102. gui.setTitle("Connect 4");
  103. gui.setSize(gui.WIDTH, gui.HEIGHT);
  104. gui.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
  105. gui.setLocationRelativeTo(null);
  106. gui.setVisible(true);
  107. gui.getContentPane().setBackground(new Color(242, 75, 75));
  108.  
  109. dispose();
  110. }
  111. }
  112.  
  113. public class event2 implements ActionListener
  114. {
  115. public void actionPerformed(ActionEvent e)
  116. {
  117. Connect4GUIAbout gui = new Connect4GUIAbout();
  118.  
  119. gui.setTitle("About");
  120. gui.setSize(gui.WIDTH, gui.HEIGHT);
  121. gui.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
  122. gui.setLocationRelativeTo(null);
  123. gui.setVisible(true);
  124. gui.setContentPane(new JLabel(new ImageIcon("res/about.png")));
  125. }
  126. }
  127.  
  128. public class event3 implements ActionListener
  129. {
  130. public void actionPerformed(ActionEvent e)
  131. {
  132. int confirm = JOptionPane.showOptionDialog(null, "Are you sure you want to close application?", "Exit Confirmation", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, null, null);
  133.  
  134. if (confirm == 0)
  135. {
  136. dispose();
  137. System.exit(0);
  138. }
  139. else
  140. {
  141. // continue
  142. }
  143. }
  144. }
  145.  
  146. public static void main (String args[])
  147. {
  148. Connect4GUI frame = new Connect4GUI();
  149.  
  150. frame.setTitle("Main Menu");
  151. frame.setSize(WIDTH, HEIGHT);
  152. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  153. frame.setLocationRelativeTo(null);
  154. frame.setVisible(true);
  155. frame.getContentPane().setBackground(new Color(242, 75, 75));
  156. }
  157. }
  158. -----------------------------------------------------------------------------------------------------------------------------------
  159. // Connect 4
  160. // Description: This class is the GUI part of the program. It is the Game Playing Section.
  161. // Sammy Samkough
  162.  
  163. import java.awt.*;
  164. import java.awt.event.*;
  165. import javax.swing.*;
  166. import java.util.*;
  167.  
  168. public class Connect4GUIStart extends JFrame
  169. {
  170. // size of screen
  171. public static final int WIDTH = 900;
  172. public static final int HEIGHT = 900;
  173.  
  174. private ImageIcon[][] board;
  175. private ImageIcon blank, red, black;
  176. private int turn;
  177. private JLabel chips;
  178.  
  179. public Connect4GUIStart()
  180. {
  181. setLayout(new FlowLayout());
  182. setSize(WIDTH, HEIGHT);
  183. getContentPane().setBackground(new Color(242, 75, 75));
  184.  
  185. blank = new ImageIcon("res/50x50/emptyspace50x50.png");
  186. red = new ImageIcon("res/50x50/redchip50x50.png");
  187. black = new ImageIcon("res/50x50/blackchip50x50.png");
  188.  
  189. // listeners
  190. addWindowListener(exitListener);
  191. chips = new JLabel(blank);
  192. chips.setIcon(blank);
  193. add(chips);
  194.  
  195. turn = 0;
  196. board = new ImageIcon[6][7];
  197. for(ImageIcon[] row: board)
  198. Arrays.fill(row, blank);
  199. }
  200.  
  201. WindowListener exitListener = new WindowAdapter()
  202. {
  203. @Override
  204. public void windowClosing(WindowEvent e)
  205. {
  206. Connect4GUI frame = new Connect4GUI();
  207.  
  208. frame.setTitle("Main Menu");
  209. frame.setSize(frame.WIDTH, frame.HEIGHT);
  210. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  211. frame.setLocationRelativeTo(null);
  212. frame.setVisible(true);
  213. frame.getContentPane().setBackground(new Color(242, 75, 75));
  214.  
  215. dispose();
  216. }
  217. };
  218. /*
  219. // Player should be player 1 or 2
  220. // slot should be the column the piece is being placed in (0-6)
  221. // Returns: an empty string unless there is an issue (invalid player or slot number, or the slot in filled up) in
  222. // which case it will return an error message
  223. public String takeTurn(int player, int slot)
  224. {
  225. if(slot < 7 && slot > -1) // Check for correct slot count
  226. {
  227. if(player == 1 || player == 2)
  228. {
  229. for(int i = board.length - 1; i >= 0; i--)
  230. {
  231. if(board[i][slot] == 0)
  232. {
  233. board[i][slot] = player;
  234. return "";
  235. }
  236. }
  237. return "ERROR: That column is full";
  238. }
  239. else
  240. return "ERROR: player input invalid (possible values: 1 or 2)";
  241. }
  242. else
  243. return "ERROR: invalid column number (possible values: 0-6)";
  244. }
  245.  
  246. // Returns number of player whos turn it is or 0 if error
  247. public int checkTurn()
  248. {
  249. int play1Count = 0;
  250. int play2Count = 0;
  251. for(int i = 0; i < board.length; i++)
  252. {
  253. for(int j = 0; j < board[i].length; j++)
  254. {
  255. if(board[i][j] == 1)
  256. play1Count++;
  257. else if(board[i][j] == 2)
  258. play2Count++;
  259. }
  260. }
  261. if(play1Count > play2Count)
  262. return 2;
  263. else if(play2Count == play1Count)
  264. return 1;
  265. return 0;
  266. }
  267.  
  268. // Returns: 0 if no win, 1 for player 1 win, 2 for player 2 win, 3 for a tie
  269. // MUST BE CALLED AFTER EVERY TURN TO AVOID HAVING BOTH PEOPLE WIN
  270. public int winDetect()
  271. {
  272. //Check for horizontal win
  273. for(int i = 0; i < board.length; i++)
  274. {
  275. for(int j = 0; j < board[i].length - 3; j++)
  276. {
  277. for(int k = 1; k < 3; k++)
  278. {
  279. if(board[i][j] == k)
  280. if(board[i][j+1] == k)
  281. if(board[i][j+2] == k)
  282. if(board[i][j+3] == k)
  283. return k;
  284. }
  285. }
  286. }
  287. // Check for vertical win
  288. for(int i = 0; i < board.length - 3; i++)
  289. {
  290. for(int j = 0; j < board[i].length; j++)
  291. {
  292. for(int k = 1; k < 3; k++)
  293. {
  294. if(board[i][j] == k)
  295. if(board[i+1][j] == k)
  296. if(board[i+2][j] == k)
  297. if(board[i+3][j] == k)
  298. return k;
  299. }
  300. }
  301. }
  302. // Check for rightup-diagnol win
  303. for(int i = board.length - 3; i < board.length; i++)
  304. {
  305. for(int j = 0; j < board[i].length - 3; j++)
  306. {
  307. for(int k = 1; k < 3; k++)
  308. {
  309. if(board[i][j] == k)
  310. if(board[i-1][j+1] == k)
  311. if(board[i-2][j+2] == k)
  312. if(board[i-3][j+3] == k)
  313. return k;
  314. }
  315. }
  316. }
  317. // Check for leftdown-diagnol win
  318. for(int i = 0; i < board.length-3; i++)
  319. {
  320. for(int j = 0; j < board[i].length-3; j++)
  321. {
  322. for(int k = 1; k < 3; k++)
  323. {
  324. if(board[i][j] == k)
  325. if(board[i+1][j+1] == k)
  326. if(board[i+2][j+2] == k)
  327. if(board[i+3][j+3] == k)
  328. return k;
  329. }
  330. }
  331. }
  332. // Check for full board (tie)
  333. int poop = 0;
  334. for(int[] row: board)
  335. for(int col: row)
  336. if(col == 0)
  337. poop++;
  338. if(poop == 0)
  339. return 3;
  340. else
  341. return 0;
  342. }
  343.  
  344. public String toString()
  345. {
  346. String result = "";
  347. for(int[] row: board)
  348. {
  349. for(int col: row)
  350. result+= col;
  351. result+= "\n";
  352. }
  353. return result;
  354. }
  355.  
  356. */
  357.  
  358. public static void main (String args[])
  359. {
  360.  
  361. }
  362. }
  363. -----------------------------------------------------------------------------------------------------------------------------------
  364. // Connect 4
  365. // Description: This class is the GUI part of the program. It is the About Section.
  366. // Sammy Samkough
  367.  
  368. import java.awt.*;
  369. import java.awt.event.*;
  370. import javax.swing.*;
  371.  
  372. public class Connect4GUIAbout extends JFrame
  373. {
  374. // size of screen
  375. public static final int WIDTH = 600;
  376. public static final int HEIGHT = 600;
  377.  
  378. public Connect4GUIAbout()
  379. {
  380. setLayout(new FlowLayout());
  381.  
  382. // listeners
  383. addWindowListener(exitListener);
  384. }
  385.  
  386. WindowListener exitListener = new WindowAdapter()
  387. {
  388. @Override
  389. public void windowClosing(WindowEvent e)
  390. {
  391. Connect4GUI frame = new Connect4GUI();
  392.  
  393. frame.setTitle("Main Menu");
  394. frame.setSize(frame.WIDTH, frame.HEIGHT);
  395. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  396. frame.setLocationRelativeTo(null);
  397. frame.setVisible(true);
  398. frame.getContentPane().setBackground(new Color(242, 75, 75));
  399.  
  400. dispose();
  401. }
  402. };
  403.  
  404. public static void main (String args[])
  405. {
  406.  
  407. }
  408. }
  409. -----------------------------------------------------------------------------------------------------------------------------------
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement