├── .gitignore ├── screenshot ├── about.PNG ├── play-with-pc.PNG ├── select-game.PNG ├── play-with-friend.PNG └── TicTacToe-photo-logo.jpg ├── src └── com │ └── houarizegai │ └── TicTacToc │ ├── images │ ├── logo.png │ ├── github.png │ ├── twitter.png │ ├── youtube.png │ └── facebook.png │ ├── AboutWindow.java │ ├── MainWindow.java │ └── PlayWindow.java ├── LICENSE └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | .idea/ 2 | out/ 3 | *.iml 4 | -------------------------------------------------------------------------------- /screenshot/about.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/HouariZegai/TicTacToe/HEAD/screenshot/about.PNG -------------------------------------------------------------------------------- /screenshot/play-with-pc.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/HouariZegai/TicTacToe/HEAD/screenshot/play-with-pc.PNG -------------------------------------------------------------------------------- /screenshot/select-game.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/HouariZegai/TicTacToe/HEAD/screenshot/select-game.PNG -------------------------------------------------------------------------------- /screenshot/play-with-friend.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/HouariZegai/TicTacToe/HEAD/screenshot/play-with-friend.PNG -------------------------------------------------------------------------------- /screenshot/TicTacToe-photo-logo.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/HouariZegai/TicTacToe/HEAD/screenshot/TicTacToe-photo-logo.jpg -------------------------------------------------------------------------------- /src/com/houarizegai/TicTacToc/images/logo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/HouariZegai/TicTacToe/HEAD/src/com/houarizegai/TicTacToc/images/logo.png -------------------------------------------------------------------------------- /src/com/houarizegai/TicTacToc/images/github.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/HouariZegai/TicTacToe/HEAD/src/com/houarizegai/TicTacToc/images/github.png -------------------------------------------------------------------------------- /src/com/houarizegai/TicTacToc/images/twitter.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/HouariZegai/TicTacToe/HEAD/src/com/houarizegai/TicTacToc/images/twitter.png -------------------------------------------------------------------------------- /src/com/houarizegai/TicTacToc/images/youtube.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/HouariZegai/TicTacToe/HEAD/src/com/houarizegai/TicTacToc/images/youtube.png -------------------------------------------------------------------------------- /src/com/houarizegai/TicTacToc/images/facebook.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/HouariZegai/TicTacToe/HEAD/src/com/houarizegai/TicTacToc/images/facebook.png -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2019 Houari Zegai 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ![tictactoe img](screenshot/TicTacToe-photo-logo.jpg) 2 | # TicTacToe 🎮 3 | A simple tic tac toe game app. 4 | 5 | ## Used language 6 | * Java (Swing). 7 | 8 | ## How to play 9 | Select type of game: 10 | * With Friend 11 | * With Computer (PC) 12 | 1. Easy 13 | 2. Medium 14 | 3. Hard 15 | 16 | **PS:** (challange for you), You can not win the computer in the **Hard** case 😀. 17 | 18 | ## Screenshots 19 | Select game | About 20 | :-------------------------:|:-------------------------: 21 | ![select game img](screenshot/select-game.PNG) | ![about img](screenshot/about.PNG) 22 | 23 | Play with friend | Play with computer 24 | :-------------------------:|:-------------------------: 25 | ![select game img](screenshot/play-with-friend.PNG) | ![about img](screenshot/play-with-pc.PNG) 26 | 27 | 28 | ## Contributing 💡 29 | If you want to contribute to this project and make it better with new ideas, your pull request is very welcomed. 30 | If you find any issue just put it in the repository issue section, thank you. 31 | 32 | .سبحَانَكَ اللَّهُمَّ وَبِحَمْدِكَ، أَشْهَدُ أَنْ لا إِلهَ إِلأَ انْتَ أَسْتَغْفِرُكَ وَأَتْوبُ إِلَيْكَ 33 | -------------------------------------------------------------------------------- /src/com/houarizegai/TicTacToc/AboutWindow.java: -------------------------------------------------------------------------------- 1 | package com.houarizegai.TicTacToc; 2 | 3 | import javax.swing.*; 4 | import java.awt.*; 5 | import java.awt.event.MouseEvent; 6 | import java.awt.event.MouseListener; 7 | import java.net.URI; 8 | 9 | public class AboutWindow extends JDialog implements MouseListener { 10 | 11 | private static JLabel labelCopyRight; // This Label Using For Write Copy Right 12 | private static JLabel labelImg[]; // This Label Contain Image Of Social Media & Account About EasyCodeTeam 13 | private static JLabel labelLogo; // This Label Using For Display Logo Of My Team 14 | private final static String IMAGE_NAME[] = {"facebook", "twitter", "youtube", "github"}; // This Table Contain The Name Of My Icon Of Social Media , And Using To Import this Icon 15 | private final static int NUMBER_OF_IMAGES = IMAGE_NAME.length; 16 | 17 | public AboutWindow() { 18 | // load the image to a imageIcon & Import Icon From WorkSpace To Replace In This Label 19 | labelLogo = new JLabel(new ImageIcon(getClass().getResource("/com/houarizegai/TicTacToc/images/logo.png"))); 20 | labelLogo.setBounds(90, 60, 150, 150); // Set The Position , Height And Width Of The Label setBounds(x, y, Width, Height) 21 | this.add(labelLogo); // Add The Label To My About Window 22 | 23 | /* This Part Bellow For Change The Label Social Media */ 24 | 25 | labelImg = new JLabel[NUMBER_OF_IMAGES]; // Initialize The Table By Four JLabel 26 | for (int i = 0; i < NUMBER_OF_IMAGES; i++) { 27 | labelImg[i] = new JLabel(new ImageIcon(getClass().getResource("/com/houarizegai/TicTacToc/images/" + IMAGE_NAME[i] + ".png"))); 28 | labelImg[i].setBounds(90 + 40 * i, 290, 32, 32); 29 | labelImg[i].addMouseListener(this); 30 | this.add(labelImg[i]); // Add My Label Image To My About Window 31 | labelImg[i].setCursor(new Cursor(Cursor.HAND_CURSOR)); // If I Move The Cursor In The LabelImg Change The Cursor Design By Hand 32 | } 33 | 34 | /* This Part Bellow For Change The Label Of Copy Right */ 35 | 36 | labelCopyRight = new JLabel("Copyright Ⓒ EasyCodeTeam, All Right Reserved 2017."); // Create New Instance From JLabel & Set By Paragraph 37 | labelCopyRight.setBounds(15, 330, 300, 30); 38 | this.add(labelCopyRight); 39 | 40 | /* This Is Parameter For My About Window */ 41 | 42 | this.setBounds(400, 200, 330, 400); 43 | this.setTitle("About This App"); // Change The Title For My Window By This Value (in argument) 44 | this.setResizable(false); // You Can Not Change The Size Of My About Window 45 | this.setAlwaysOnTop(true); // I Put My About Window Always In Top 46 | this.setLayout(null); // Disable All Layout Because I Control The Position Of My Elements By The Function setBounds(x, y, Width, Height) 47 | this.setVisible(true); // Show The Window To My Screen 48 | } 49 | 50 | @Override 51 | public void mouseClicked(MouseEvent e) { 52 | for(int i = 0; i < labelImg.length; i++) 53 | if (e.getSource().equals(labelImg[i])) 54 | try { 55 | Desktop.getDesktop().browse(new URI("https://www." + IMAGE_NAME[i] + ".com/HouariZegai")); 56 | } catch (Exception ex) { 57 | 58 | } 59 | } 60 | 61 | @Override 62 | public void mousePressed(MouseEvent e) { 63 | 64 | } 65 | 66 | @Override 67 | public void mouseReleased(MouseEvent e) { 68 | 69 | } 70 | 71 | @Override 72 | public void mouseEntered(MouseEvent e) { 73 | 74 | } 75 | 76 | @Override 77 | public void mouseExited(MouseEvent e) { 78 | 79 | } 80 | 81 | 82 | public static void main(String[] args) { 83 | new AboutWindow().setVisible(true); 84 | } 85 | } -------------------------------------------------------------------------------- /src/com/houarizegai/TicTacToc/MainWindow.java: -------------------------------------------------------------------------------- 1 | package com.houarizegai.TicTacToc; 2 | 3 | import javax.swing.*; 4 | import java.awt.Font; 5 | import java.awt.event.ActionEvent; 6 | import java.awt.event.ActionListener; 7 | 8 | 9 | public class MainWindow extends JFrame implements ActionListener{ 10 | 11 | private final JLabel labelSelect; // OutPut String In Screen 12 | private JLabel labelChoix; // Print String In Screen 13 | private final JButton btnWithFriend; // Button Play With Friend 14 | private final JButton btnWithPc; // Button Play With PC 15 | private final JButton btnAbout; // Button About This Application 16 | private JRadioButton choixWithPc[] = new JRadioButton[3]; // RadioButton Choix: 0:Easy, 1:Medium & :2Hard 17 | private boolean showHidePcPlay = false; // This Boolean Using For Show or Hide Choix 18 | private final Font FontForLabel = new Font("Comic Sans MS", Font.PLAIN, 16); // Create New Instance for Change this Value: Font(Font-Family, Font-Weight, Font-Size) 19 | private static int i = 0; // This Variable Using In Loop 20 | 21 | MainWindow() { 22 | 23 | labelSelect = new JLabel(PlayWindow.setColor("Please Select Type Of Game :", "blue")); // Create New Instance From JLabel & Add + Change The Color For The Text 24 | labelSelect.setBounds(20, 40, 315, 50); // Set The Position , Height And Width Of The Label setBounds(x, y, Width, Height) 25 | labelSelect.setFont(new Font("Comic Sans Ms", Font.PLAIN, 15)); // Change The Font : (Font-Family, Font-Weight, Font-Size) 26 | this.add(labelSelect); // Add The Label To My About Window 27 | 28 | btnWithFriend = new JButton("Play With My Friend"); 29 | btnWithFriend.setBounds(20, 100, 250, 50); 30 | btnWithFriend.setFont(FontForLabel); 31 | btnWithFriend.addActionListener(event -> { // If I Click This Button Call This Block To Execute 32 | new PlayWindow(0); // Create New Window From The Class PlayWithFriend 33 | this.setVisible(false); // Hide My Main Window 34 | }); 35 | this.add(btnWithFriend); 36 | 37 | btnWithPc = new JButton("Play With PC"); 38 | btnWithPc.setBounds(20, 160, 250, 50); 39 | btnWithPc.setFont(FontForLabel); 40 | btnWithPc.addActionListener(event -> { // If I Click To "With PC" Button Execute This Block ( Show Or Hide Choix & Resize The Main Window ) 41 | showHidePcPlay = (showHidePcPlay)? false : true; // Inverse The Value Of The Variable By True Or False 42 | labelChoix.setVisible(showHidePcPlay); // Show Or Hide The labelChoix 43 | for(i = 0; i < 3; i++) 44 | choixWithPc[i].setVisible(showHidePcPlay); // Show Or Hide The JRadioButton 45 | 46 | this.setSize(310, (showHidePcPlay)?450:270); 47 | }); 48 | this.add(btnWithPc); 49 | 50 | labelChoix = new JLabel(PlayWindow.setColor("Please Select Level :", "green")); 51 | labelChoix.setBounds(30, 220, 250, 50); 52 | labelChoix.setVisible(false); 53 | this.add(labelChoix); 54 | 55 | choixWithPc[0] = new JRadioButton("Easy"); 56 | choixWithPc[1] = new JRadioButton("Medium"); 57 | choixWithPc[2] = new JRadioButton("Hard"); 58 | 59 | for(int i = 0; i < 3; i++) { 60 | choixWithPc[i].setBounds(40, 260 + 30 * i, 150, 30); 61 | choixWithPc[i].setVisible(false); 62 | choixWithPc[i].addActionListener(this); 63 | this.add(choixWithPc[i]); 64 | } 65 | 66 | btnAbout = new JButton("About"); 67 | btnAbout.setBounds(200, 10, 70, 25); 68 | btnAbout.addActionListener(event -> { 69 | new AboutWindow(); 70 | }); 71 | this.add(btnAbout); 72 | 73 | setTitle("TicTacToc Game"); // Edit (Set) The Title Of My Main Window 74 | setBounds(400, 170, 310, 270); 75 | setLayout(null); // Disable All Layout Because I Control The Position Of My Elements By The Function setBounds(x, y, Width, Height) 76 | setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // If I Click To The Close Button Stop Process 77 | setVisible(true); // Show The Window To The Screen 78 | setIconImage(new ImageIcon(getClass().getResource("/com/houarizegai/TicTacToc/images/logo.png")).getImage()); 79 | } 80 | 81 | @Override 82 | public void actionPerformed(ActionEvent e) { 83 | for(int i = 0; i < 3; i++) 84 | if(e.getSource() == choixWithPc[i]) { 85 | new PlayWindow(i+1); 86 | this.setVisible(false); 87 | } 88 | } 89 | 90 | public static void main(String[] args) { 91 | new MainWindow(); // Create New Instance (Window) From Main Class 92 | } 93 | } -------------------------------------------------------------------------------- /src/com/houarizegai/TicTacToc/PlayWindow.java: -------------------------------------------------------------------------------- 1 | package com.houarizegai.TicTacToc; 2 | 3 | import java.awt.Color; 4 | import java.awt.Font; 5 | import java.awt.event.ActionEvent; 6 | import java.awt.event.ActionListener; 7 | import java.util.Random; 8 | 9 | import javax.swing.JButton; 10 | import javax.swing.JFrame; 11 | import javax.swing.JLabel; 12 | import javax.swing.JOptionPane; 13 | 14 | public class PlayWindow extends JFrame implements ActionListener{ 15 | 16 | private JLabel TableOfScore; // Print Table Of Score 17 | private int xScore = 0; // This Value Is The Point Wined By X 18 | private int oScore = 0; // This Value Is The Point Wined By O 19 | private JButton buttonsXO[]; // This Button Using For Input XO 20 | private JButton btnReset; // This Button Using For Clear Screen & Reset Score 21 | private JButton btnClear; // This Button Using For Clean Screen 22 | private JButton btnBackToMain; // This Button Using For Back To The Main Window 23 | private final static int BUTTON_XO_WIDTH = 80; // The Width Of The Button XO 24 | private final static int BUTTON_XO_HEIGHT = 80; // The Height Of The Button XO 25 | private final static int POSITION_XO_H[] = {30, BUTTON_XO_WIDTH * 1 + 30, BUTTON_XO_WIDTH * 2 + 30, BUTTON_XO_WIDTH * 3 + 30}; // Dimension Of Button Horizontal 26 | private final static int POSITION_XO_V[] = {120, BUTTON_XO_WIDTH * 1 + 120, BUTTON_XO_WIDTH * 2 + 120, BUTTON_XO_WIDTH * 3 + 120 + 20}; // Dimontion Of Button Vertical & The Value 20 For Margin-Top 27 | private static int i = 0; // This Is Counter Using in Loop 28 | 29 | private final int CHOIX_LEVEL; 30 | /* This Constants Using For Check The Game If It Is WithFriend, Easy , Medium Or Hard */ 31 | private final int CHOIX_FRIEND = 0; 32 | private final int CHOIX_EASY = 1; 33 | private final int CHOIX_MEDIUM = 2; 34 | private final int CHOIX_HARD = 3; 35 | 36 | /* This Variables Using In Case Play With Friend */ 37 | private static boolean player1 = true; // This Variable Tell Me Witch Player Play Now 38 | 39 | /* This Variables Using In Case Play With PC Level Easy */ 40 | private static Random rand = new Random(); // Get Random Value 41 | private boolean printRand = true; // Check This Value if True Print XO For PC 42 | 43 | /* This Variables Using In Case Play With PC Level Medium OR Hard*/ 44 | private int arrayRows[] = new int[8]; // This Table is add : ( 1 For X ) And ( -1 For O ) 45 | private boolean mCenterFirst = false; 46 | private boolean mCornerFirst = false; 47 | private boolean mHvFirst = false; 48 | private boolean mHvAfterCorner = false; 49 | private boolean mCornerAfterHv = false; 50 | private boolean mCornerAfterCenter = false; 51 | private boolean mHvAfterHv = false; 52 | private int mCounter = 0; 53 | private boolean mCorner = false; 54 | private boolean mHvAfterCenter = false; 55 | 56 | PlayWindow(int CHOIX_LEVEL) { // CHOIX_LEVEL = 0:WithFriend, 1:Easy, 2:Medium, 3:Hard 57 | 58 | this.CHOIX_LEVEL = CHOIX_LEVEL; 59 | 60 | if (this.CHOIX_LEVEL == CHOIX_FRIEND) 61 | this.setTitle("Play With Friend"); 62 | else if (this.CHOIX_LEVEL == CHOIX_EASY) 63 | this.setTitle("Play With PC Easy"); 64 | else if (this.CHOIX_LEVEL == CHOIX_MEDIUM) 65 | this.setTitle("Play With PC Medium"); 66 | else 67 | this.setTitle("Play With PC Hard"); 68 | 69 | TableOfScore = new JLabel(""); 70 | printScore(xScore, oScore); // Print Format 71 | TableOfScore.setBounds(170, 10, 200, 100); // Dimension 72 | TableOfScore.setFont(new Font("Comic Sans MS", Font.PLAIN, 16)); // change Font Of Label ( FontFamily , FontWeight , FontSize ) 73 | this.add(TableOfScore); 74 | 75 | buttonsXO = new JButton[9]; 76 | 77 | for (i = 0; i < buttonsXO.length; i++) { // This Loop For Initialize & Modify The Size & Position Of The Button & Adding To The Screen 78 | buttonsXO[i] = new JButton(); 79 | if (i < 3) { 80 | buttonsXO[i].setBounds(POSITION_XO_H[i], POSITION_XO_V[0], BUTTON_XO_WIDTH, BUTTON_XO_HEIGHT); 81 | } else if (i < 6) { 82 | buttonsXO[i].setBounds(POSITION_XO_H[i - 3], POSITION_XO_V[1], BUTTON_XO_WIDTH, BUTTON_XO_HEIGHT); 83 | } else if (i < 9) { 84 | buttonsXO[i].setBounds(POSITION_XO_H[i - 6], POSITION_XO_V[2], BUTTON_XO_WIDTH, BUTTON_XO_HEIGHT); 85 | } 86 | 87 | buttonsXO[i].setFont(new Font("Comic Sans MS", Font.PLAIN, 26)); 88 | buttonsXO[i].setBackground(Color.WHITE); // Change The Color of The Button 89 | this.add(buttonsXO[i]); 90 | } 91 | for (i = 0; i < 9; i++) { 92 | buttonsXO[i].addActionListener(this); 93 | } 94 | 95 | btnReset = new JButton("Reset"); 96 | btnReset.setBounds(POSITION_XO_H[0], POSITION_XO_V[3], BUTTON_XO_WIDTH * 3 / 2 - 10, 40); 97 | btnReset.setFont(new Font("Comic Sans MS", Font.PLAIN, 16)); 98 | btnReset.addActionListener(event -> { 99 | clear(); 100 | resetScore(); 101 | }); 102 | this.add(btnReset); 103 | 104 | btnClear = new JButton("Clear"); 105 | btnClear.setBounds(POSITION_XO_H[0] + btnReset.getWidth() + 20, POSITION_XO_V[3], BUTTON_XO_WIDTH * 3 / 2 - 10, 40); 106 | btnClear.setFont(new Font("Comic Sans MS", Font.PLAIN, 16)); 107 | btnClear.addActionListener(event -> { 108 | clear(); 109 | }); 110 | this.add(btnClear); 111 | 112 | btnBackToMain = new JButton("Back To Main"); 113 | btnBackToMain.setBounds(POSITION_XO_H[0], POSITION_XO_V[3] + 50, BUTTON_XO_WIDTH * 3, 40); 114 | btnBackToMain.setFont(new Font("Comic Sans MS", Font.PLAIN, 16)); 115 | btnBackToMain.addActionListener(event -> { 116 | this.setVisible(false); 117 | new MainWindow(); 118 | }); 119 | add(btnBackToMain); 120 | 121 | this.setBounds(400, 170, buttonsXO[0].getWidth() * 3 + 75, TableOfScore.getHeight() + buttonsXO[0].getHeight() * 3 + btnReset.getHeight() + btnBackToMain.getHeight() + 120); 122 | this.setLayout(null); 123 | this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 124 | this.setVisible(true); 125 | } 126 | 127 | @Override 128 | public void actionPerformed(ActionEvent e){ 129 | 130 | if (CHOIX_LEVEL == CHOIX_FRIEND) { 131 | for(i = 0; i < 9; i++){ 132 | if (e.getSource().equals(buttonsXO[i])) // If ButtonsXO[i] Is Clicked ? 133 | printXOForFriend(i); // Fill The ButtonXO In The Index i By X Or O 134 | } 135 | }else if (CHOIX_LEVEL == CHOIX_EASY) { 136 | for (i = 0; i < 9; i++) { 137 | if (e.getSource() == buttonsXO[i]) 138 | printXOForEasy(i); 139 | } 140 | }else if (CHOIX_LEVEL == CHOIX_MEDIUM) { 141 | for (i = 0; i < 9; i++) 142 | if (e.getSource() == buttonsXO[i]){ 143 | if (buttonsXO[i].getText().equals("")) { 144 | printXOForMeMedium(i); 145 | 146 | if(getResult(true)) { 147 | printXOForPcMedium(); 148 | getResult(false); 149 | } 150 | } 151 | } 152 | }else if (CHOIX_LEVEL == CHOIX_HARD) { 153 | for (i = 0; i < 9; i++) 154 | if (e.getSource() == buttonsXO[i]){ 155 | if (buttonsXO[i].getText().equals("")) { 156 | printXOForMeHard(i); 157 | 158 | if(getResult(true)) { 159 | printXOForPcHard(); 160 | getResult(false); 161 | } 162 | } 163 | } 164 | } 165 | 166 | } 167 | 168 | /* This Function Bellow Using In All Play Case */ 169 | 170 | private void printScore(int x, int o) { // This Function For Modify Table of Score 171 | String xFormat = setColorOnly(String.valueOf(x), "green"), 172 | oFormat = setColorOnly(String.valueOf(o), "green"); 173 | if (x > o) { 174 | xFormat = setColorOnly(String.valueOf(x), "green"); 175 | oFormat = setColorOnly(String.valueOf(o), "red"); 176 | } else if (x < o) { 177 | xFormat = setColorOnly(String.valueOf(x), "red"); 178 | oFormat = setColorOnly(String.valueOf(o), "green"); 179 | } 180 | 181 | String p1 = "You"; 182 | String p2 = "Pc"; 183 | 184 | if (CHOIX_LEVEL == CHOIX_FRIEND) { 185 | p1 = "X"; 186 | p2 = "O"; 187 | } 188 | 189 | TableOfScore.setText("" 190 | + "" 191 | + ""); 192 | } 193 | 194 | private boolean getResult(boolean Player1Win) { // This Function Show A dialog If Any Player Win Or If All Button fill ( Null ) 195 | if (((buttonsXO[0].getText().equals(buttonsXO[3].getText())) && (buttonsXO[0].getText().equals(buttonsXO[6].getText())) && (!buttonsXO[0].getText().equals(""))) 196 | || ((buttonsXO[1].getText().equals(buttonsXO[4].getText())) && (buttonsXO[1].getText().equals(buttonsXO[7].getText())) && (!buttonsXO[1].getText().equals(""))) 197 | || ((buttonsXO[2].getText().equals(buttonsXO[5].getText())) && (buttonsXO[2].getText().equals(buttonsXO[8].getText())) && (!buttonsXO[2].getText().equals(""))) 198 | || ((buttonsXO[0].getText().equals(buttonsXO[1].getText())) && (buttonsXO[0].getText().equals(buttonsXO[2].getText())) && (!buttonsXO[0].getText().equals(""))) 199 | || ((buttonsXO[3].getText().equals(buttonsXO[4].getText())) && (buttonsXO[3].getText().equals(buttonsXO[5].getText())) && (!buttonsXO[3].getText().equals(""))) 200 | || ((buttonsXO[6].getText().equals(buttonsXO[7].getText())) && (buttonsXO[6].getText().equals(buttonsXO[8].getText())) && (!buttonsXO[6].getText().equals(""))) 201 | || ((buttonsXO[0].getText().equals(buttonsXO[4].getText())) && (buttonsXO[0].getText().equals(buttonsXO[8].getText())) && (!buttonsXO[0].getText().equals(""))) 202 | || ((buttonsXO[2].getText().equals(buttonsXO[4].getText())) && (buttonsXO[2].getText().equals(buttonsXO[6].getText())) && (!buttonsXO[2].getText().equals("")))) { 203 | 204 | if (Player1Win) { 205 | xScore++; 206 | if (CHOIX_LEVEL == CHOIX_FRIEND) 207 | JOptionPane.showMessageDialog(null, "" + setColorOnly("Player 1 (X) ", "green") + setColorOnly("Win !" , "green") + ""); 208 | else 209 | JOptionPane.showMessageDialog(null, setColor("You Win ^_^ !" , "green")); 210 | } else { 211 | oScore++; 212 | if (CHOIX_LEVEL == CHOIX_FRIEND) 213 | JOptionPane.showMessageDialog(null, "" + setColorOnly("Player 2 (O) ", "blue") + setColorOnly("Win !" , "green") + ""); 214 | else 215 | JOptionPane.showMessageDialog(null, "" + setColorOnly("You Lose : (", "red") + "
PC " + setColor("Win !", "green") + ""); 216 | } 217 | printScore(xScore, oScore); 218 | clear(); 219 | return false; 220 | } else { 221 | if (!buttonsXO[0].getText().equals("") 222 | && !buttonsXO[1].getText().equals("") 223 | && !buttonsXO[2].getText().equals("") 224 | && !buttonsXO[3].getText().equals("") 225 | && !buttonsXO[4].getText().equals("") 226 | && !buttonsXO[5].getText().equals("") 227 | && !buttonsXO[6].getText().equals("") 228 | && !buttonsXO[7].getText().equals("") 229 | && !buttonsXO[8].getText().equals("")) { 230 | JOptionPane.showMessageDialog(null, " Draw !"); 231 | clear(); 232 | return false; 233 | } else { 234 | printRand = true; 235 | } 236 | } 237 | return true; 238 | } 239 | 240 | private void clear() { // This Function For Clean The Screen & Initialize All Variables 241 | for (i = 0; i < 9; i++) { 242 | buttonsXO[i].setText(""); // Remove The Contain Of ButtonXO 243 | } 244 | if(CHOIX_LEVEL == CHOIX_EASY) // This Is For Easy Case 245 | printRand = false; 246 | else { // This Is For Medium OR Hard Case 247 | for (i = 0; i < 8; i++) 248 | arrayRows[i] = 0; 249 | mCenterFirst = false; 250 | mCornerFirst = false; 251 | mHvFirst = false; 252 | mHvAfterCorner = false; 253 | mCornerAfterHv = false; 254 | mCornerAfterCenter = false; 255 | mHvAfterHv = false; 256 | mCorner = false; 257 | mHvAfterCenter = false; 258 | mCounter = 0; 259 | } 260 | } 261 | 262 | private void resetScore() { // Call Clear() Function & This Function For initialize The Score 263 | clear(); // Clean The ButtonsXO 264 | printScore(xScore = 0, oScore = 0); // Initialize The Table Of Score & The Variables (xScore & oScore) By 0 265 | } 266 | 267 | protected static String setColor(String before, String color) { // Change Color Of String 268 | return "" + before + ""; 269 | } 270 | 271 | private static String setColorOnly(String before, String color) { // Change Color Of String But With Out Add HTML Tag (Before & After The String) 272 | return "" + before + ""; 273 | } 274 | 275 | /* This Function Bellow Using In Play With Friend Case */ 276 | 277 | private void printXOForFriend(int index) { // Fill The ButtonXO[index] By XO & Check If Any Player Win ( Call The Function getResult() ) 278 | if (buttonsXO[index].getText().equals("")) { 279 | if (player1) { // If Player 1 Is The Tour For Play 280 | buttonsXO[index].setText(setColor("X", "green")); 281 | getResult(player1); // Check If The Player1 Win After Playing 282 | player1 = false; 283 | } else { // If Player 2 Is The Tour For Play 284 | buttonsXO[index].setText(setColor("O", "blue")); 285 | getResult(player1); // Check If The Player2 Win After Playing 286 | player1 = true; 287 | } 288 | } 289 | } 290 | 291 | /* This Function Bellow Using In Easy Case */ 292 | 293 | private void printXOForEasy(int i) { // Fill The Gaps By XO & Call The Function getResult() 294 | if (buttonsXO[i].getText().equals("")) { // Check If The ButtonXO Is Empty To Filling By X or O 295 | buttonsXO[i].setText(setColor("X", "green")); 296 | getResult(true); // Check If I Wined ! 297 | 298 | if (printRand) { 299 | while (true) { // Execute This Block Until Pc Fill 1 Gaps By The Random Value 300 | int index = rand.nextInt(9); // Get Random Value Between 0 And 8 301 | if (buttonsXO[index].getText().equals("")) { 302 | try{ 303 | Thread.sleep(100); // Stop Execution 100 MilliSeconds (0.1 Seconds) 304 | }catch (InterruptedException e){} 305 | 306 | buttonsXO[index].setText(setColor("O", "blue")); 307 | getResult(false); // Check If The Pc Wined ! 308 | break; 309 | } 310 | } 311 | } 312 | } 313 | } 314 | 315 | /* This Function Bellow Using For Medium & Hard Case */ 316 | 317 | private void printXOForMeMedium(int index) { 318 | buttonsXO[index].setText(setColor("X", "green")); 319 | if (index == 4) { 320 | if (mCounter == 0) { // Check If The First Checked Is The Center 321 | mCenterFirst = true; 322 | } 323 | } else if (index == 0 || index == 2 || index == 6 || index == 8) { // The Corner 324 | mCorner = true; 325 | } else { 326 | mCorner = false; 327 | } 328 | fillArray(index, 1); 329 | mCounter++; 330 | } 331 | 332 | private void printXOForMeHard(int index) { // Fill The Gaps By 0 & Call The Function getResult() 333 | buttonsXO[index].setText(setColor("X", "green")); 334 | if (index == 4) { 335 | if (mCounter == 0) { // Check If The First Checked Is The Center 336 | mCenterFirst = true; 337 | } 338 | } else if (index == 0 || index == 2 || index == 6 || index == 8) { // Les Courner 339 | if (mCounter == 0) { // Check If The First Checked Is The Corner 340 | mCornerFirst = true; 341 | } else { 342 | if (mCenterFirst) { 343 | mCornerAfterCenter = true; 344 | mCenterFirst = false; 345 | } 346 | mCorner = true; 347 | mCornerFirst = false; 348 | if (mHvFirst) { 349 | mCornerAfterHv = true; 350 | } 351 | mHvFirst = false; 352 | } 353 | } else { 354 | if (mCounter == 0) { 355 | mHvFirst = true; 356 | } else { 357 | if (mCornerFirst) { 358 | mHvAfterCorner = true; 359 | mCornerFirst = false; 360 | } else if (mHvFirst) { 361 | mHvAfterHv = true; 362 | mHvFirst = false; 363 | } else if (mCenterFirst) { 364 | mHvAfterCenter = true; 365 | mCornerAfterCenter = false; 366 | mCenterFirst = false; 367 | } else { 368 | mCorner = false; 369 | } 370 | } 371 | } 372 | fillArray(index, 1); 373 | mCounter++; 374 | } 375 | 376 | private void printXOForPcMedium() { 377 | if (mCenterFirst) { 378 | //TODO play in corner 379 | if (buttonsXO[0].getText().equals("")) { 380 | fillCasePc(0); 381 | } else if (buttonsXO[2].getText().equals("")) { 382 | fillCasePc(2); 383 | } else if (buttonsXO[6].getText().equals("")) { 384 | fillCasePc(6); 385 | } else if (buttonsXO[8].getText().equals("")) { 386 | fillCasePc(8); 387 | } 388 | mCenterFirst = false; 389 | } else if (buttonsXO[4].getText().equals("")) { 390 | fillCasePc(4); 391 | } else if (meWantWin() != -1) { 392 | //TODO go and win 393 | int indexOfTheRow = meWantWin(); 394 | int[] theTargetRow = getTargetRow(indexOfTheRow); 395 | int indexOfTheTargetCase = getIndexOfTheEmptyCase(theTargetRow); 396 | fillCasePc(indexOfTheTargetCase); 397 | mCorner = false; 398 | mCornerAfterCenter = false; 399 | mHvAfterHv = false; 400 | mHvAfterCorner = false; 401 | mCornerAfterHv = false; 402 | } else if (adverserWantWin() != -1) { 403 | //TODO stop him 404 | int indexOfTheRow = adverserWantWin(); 405 | int[] theTargetRow = getTargetRow(indexOfTheRow); 406 | int indexOfTheTargetCase = getIndexOfTheEmptyCase(theTargetRow); 407 | fillCasePc(indexOfTheTargetCase); 408 | mCorner = false; 409 | mCornerAfterCenter = false; 410 | mHvAfterHv = false; 411 | mHvAfterCorner = false; 412 | mCornerAfterHv = false; 413 | } else if (mCorner) { 414 | //TODO play in hv 415 | if (buttonsXO[1].getText().equals("")) { 416 | fillCasePc(1); 417 | } else if (buttonsXO[3].getText().equals("")) { 418 | fillCasePc(3); 419 | } else if (buttonsXO[5].getText().equals("")) { 420 | fillCasePc(5); 421 | } else if (buttonsXO[7].getText().equals("")) { 422 | fillCasePc(7); 423 | } 424 | } else { 425 | if (buttonsXO[0].getText().equals("")) { 426 | fillCasePc(0); 427 | } else if (buttonsXO[2].getText().equals("")) { 428 | fillCasePc(2); 429 | } else if (buttonsXO[6].getText().equals("")) { 430 | fillCasePc(6); 431 | } else if (buttonsXO[8].getText().equals("")) { 432 | fillCasePc(8); 433 | } 434 | } 435 | } 436 | 437 | private void printXOForPcHard() { 438 | if (mCenterFirst) { 439 | //TODO play in corner 440 | if (buttonsXO[0].getText().equals("")) { 441 | fillCasePc(0); 442 | } else if (buttonsXO[2].getText().equals("")) { 443 | fillCasePc(2); 444 | } else if (buttonsXO[6].getText().equals("")) { 445 | fillCasePc(6); 446 | } else if (buttonsXO[8].getText().equals("")) { 447 | fillCasePc(8); 448 | } 449 | } else if (mCornerFirst) { 450 | //TODO play in center 451 | fillCasePc(4); 452 | } else if (mHvFirst) { 453 | fillCasePc(4); 454 | //TODO play4); 455 | } else if (mHvAfterHv) { 456 | int indexOfTheBestCorner = getBestCorner(); 457 | fillCasePc(indexOfTheBestCorner); 458 | mCorner = false; 459 | mHvAfterHv = false; 460 | //TODO play in best Corner 461 | } else if (mCornerAfterCenter) { 462 | if (buttonsXO[0].getText().equals("")) { 463 | fillCasePc(0); 464 | } else if (buttonsXO[2].getText().equals("")) { 465 | fillCasePc(2); 466 | } else if (buttonsXO[6].getText().equals("")) { 467 | fillCasePc(6); 468 | } else if (buttonsXO[8].getText().equals("")) { 469 | fillCasePc(8); 470 | } 471 | mCornerAfterCenter = false; 472 | //todo play in corner 473 | } else if (meWantWin() != -1) { 474 | //TODO go and win 475 | int indexOfTheRow = meWantWin(); 476 | int[] theTargetRow = getTargetRow(indexOfTheRow); 477 | int indexOfTheTargetCase = getIndexOfTheEmptyCase(theTargetRow); 478 | fillCasePc(indexOfTheTargetCase); 479 | mCorner = false; 480 | mCornerAfterCenter = false; 481 | mHvAfterHv = false; 482 | mHvAfterCorner = false; 483 | mCornerAfterHv = false; 484 | } else if (adverserWantWin() != -1) { 485 | //TODO stop him 486 | int indexOfTheRow = adverserWantWin(); 487 | int[] theTargetRow = getTargetRow(indexOfTheRow); 488 | int indexOfTheTargetCase = getIndexOfTheEmptyCase(theTargetRow); 489 | fillCasePc(indexOfTheTargetCase); 490 | mCorner = false; 491 | mCornerAfterCenter = false; 492 | mHvAfterHv = false; 493 | mHvAfterCorner = false; 494 | mCornerAfterHv = false; 495 | } else if (mCornerAfterHv) { 496 | int indexOfBestHV = getBestHV(); 497 | fillCasePc(indexOfBestHV); 498 | mCorner = false; 499 | mCornerAfterHv = false; 500 | //TODO play in best Corner en face 501 | } else if (mHvAfterCorner) { 502 | int indexOfBestHV = getBestHV(); 503 | fillCasePc(indexOfBestHV); 504 | mCorner = false; 505 | mHvAfterCorner = false; 506 | } else if (mCorner) { 507 | //TODO play in hv 508 | if (buttonsXO[1].getText().equals("")) { 509 | fillCasePc(1); 510 | } else if (buttonsXO[3].getText().equals("")) { 511 | fillCasePc(3); 512 | } else if (buttonsXO[5].getText().equals("")) { 513 | fillCasePc(5); 514 | } else if (buttonsXO[7].getText().equals("")) { 515 | fillCasePc(7); 516 | } 517 | } else { 518 | if (mCounter <= 4) { 519 | int indexOfTheBestCorner = getBestCorner(); 520 | fillCasePc(indexOfTheBestCorner); 521 | mCorner = false; 522 | //TODO play in best corner 523 | 524 | } else { 525 | if (buttonsXO[0].getText().equals("")) { 526 | fillCasePc(0); 527 | } else if (buttonsXO[2].getText().equals("")) { 528 | fillCasePc(2); 529 | } else if (buttonsXO[6].getText().equals("")) { 530 | fillCasePc(6); 531 | } else if (buttonsXO[8].getText().equals("")) { 532 | fillCasePc(8); 533 | } 534 | } 535 | } 536 | } 537 | 538 | private void fillArray(int cases, int current) { 539 | switch (cases) { 540 | case 0: 541 | arrayRows[0] += current; 542 | arrayRows[3] += current; 543 | arrayRows[7] += current; 544 | break; 545 | case 1: 546 | arrayRows[0] += current; 547 | arrayRows[4] += current; 548 | break; 549 | case 2: 550 | arrayRows[0] += current; 551 | arrayRows[5] += current; 552 | arrayRows[6] += current; 553 | break; 554 | case 3: 555 | arrayRows[1] += current; 556 | arrayRows[3] += current; 557 | break; 558 | case 4: 559 | arrayRows[1] += current; 560 | arrayRows[4] += current; 561 | arrayRows[6] += current; 562 | arrayRows[7] += current; 563 | break; 564 | case 5: 565 | arrayRows[1] += current; 566 | arrayRows[5] += current; 567 | break; 568 | case 6: 569 | arrayRows[2] += current; 570 | arrayRows[3] += current; 571 | arrayRows[6] += current; 572 | break; 573 | case 7: 574 | arrayRows[2] += current; 575 | arrayRows[4] += current; 576 | break; 577 | case 8: 578 | arrayRows[2] += current; 579 | arrayRows[5] += current; 580 | arrayRows[7] += current; 581 | break; 582 | } 583 | } 584 | 585 | private void fillCasePc(int index){ 586 | try{ 587 | Thread.sleep(100); 588 | }catch(InterruptedException e){} 589 | 590 | buttonsXO[index].setText(setColor("O", "blue")); 591 | fillArray(index, -1); 592 | mCounter++; 593 | mCorner = false; 594 | } 595 | 596 | private int adverserWantWin() { 597 | int index = -1; 598 | for (int i = 0; i < arrayRows.length; i++) 599 | index = (arrayRows[i] == 2)? i: index; 600 | return index; 601 | } 602 | 603 | private int meWantWin() { 604 | int index = -1; 605 | for (int i = 0; i < arrayRows.length; i++) 606 | index = (arrayRows[i] == -2)? i : index; 607 | return index; 608 | } 609 | 610 | private static int[] getTargetRow(int index) { 611 | switch (index) { 612 | case 0: 613 | return new int[]{0, 1, 2}; 614 | case 1: 615 | return new int[]{3, 4, 5}; 616 | case 2: 617 | return new int[]{6, 7, 8}; 618 | case 3: 619 | return new int[]{0, 3, 6}; 620 | case 4: 621 | return new int[]{1, 4, 7}; 622 | case 5: 623 | return new int[]{2, 5, 8}; 624 | case 6: 625 | return new int[]{2, 4, 6}; 626 | case 7: 627 | return new int[]{0, 4, 8}; 628 | default: 629 | return new int[]{}; 630 | } 631 | } 632 | 633 | private int getIndexOfTheEmptyCase(int[] array) { 634 | int index = -1; 635 | for (int i = 0; i < 3; i++) 636 | if (buttonsXO[array[i]].getText().equals("")) 637 | index = array[i]; 638 | 639 | return index; 640 | } 641 | 642 | private int getBestHV() { 643 | int ZeroFour = arrayRows[0] + arrayRows[4]; 644 | int TwoFour = arrayRows[2] + arrayRows[4]; 645 | int OneThree = arrayRows[1] + arrayRows[3]; 646 | int OneFive = arrayRows[1] + arrayRows[5]; 647 | 648 | if ((ZeroFour <= OneFive) && (ZeroFour <= TwoFour) && (ZeroFour <= OneThree)) { 649 | return 1; 650 | } else if ((TwoFour <= OneThree) && (TwoFour <= ZeroFour) && (TwoFour <= OneFive)) { 651 | return 7; 652 | } else if ((OneThree <= TwoFour) && (OneThree <= ZeroFour) && (OneThree <= OneFive)) { 653 | return 3; 654 | } else { 655 | return 5; 656 | } 657 | } 658 | 659 | private int getBestCorner() { 660 | int ZeroThree = arrayRows[0] + arrayRows[3]; 661 | int TwoThree = arrayRows[2] + arrayRows[3]; 662 | int ZeroFive = arrayRows[0] + arrayRows[5]; 663 | int TwoFive = arrayRows[2] + arrayRows[5]; 664 | 665 | if ((ZeroThree >= TwoThree) && (ZeroThree >= ZeroFive) && (ZeroThree >= TwoFive)) { 666 | return 0; 667 | } else if ((TwoThree >= ZeroThree) && (TwoThree >= ZeroFive) && (TwoThree >= TwoFive)) { 668 | return 6; 669 | } else if ((ZeroFive >= ZeroThree) && (ZeroFive >= TwoThree) && (ZeroFive >= TwoFive)) { 670 | return 2; 671 | } else { 672 | return 8; 673 | } 674 | } 675 | } --------------------------------------------------------------------------------
Score :   
"+ p1 + "" + xFormat + "
"+ p2 + "" + oFormat + "