{"id":103410,"date":"2023-01-20T10:30:21","date_gmt":"2023-01-20T10:30:21","guid":{"rendered":"https:\/\/gkscientist.com\/?p=103410"},"modified":"2023-01-20T10:30:26","modified_gmt":"2023-01-20T10:30:26","slug":"java-tic-tac-toe-game-project","status":"publish","type":"post","link":"https:\/\/gkscientist.com\/java-tic-tac-toe-game-project\/","title":{"rendered":"Java Tic Tac Toe Game Project"},"content":{"rendered":"\n

Java Tic Tac Toe Game Project:<\/h2>\n\n\n\n
We can use the PyCharm code editor for this example. If you do not know about it then follow this link-\u00a0How to install PyCharm for Python and create a program in it<\/em><\/strong><\/a>.<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n

Java Tic Tac Toe Game Project Sample Code:<\/em><\/strong><\/p>\n\n\n\n

\"TicTacToe<\/figure>\n\n\n\n
\"TicTacToe<\/figure>\n\n\n\n
\"TicTacToe<\/figure>\n\n\n\n
\"TicTacToe<\/figure>\n\n\n\n
\"TicTacToe<\/figure>\n\n\n\n
\"TicTacToe<\/figure>\n\n\n\n
\"TicTacToe<\/figure>\n\n\n\n

Output:<\/em><\/strong><\/p>\n\n\n\n

\"TicTacToe<\/figure>\n\n\n\n
import java.awt.*;\r\nimport java.awt.event.*;\r\nimport java.util.*;\r\nimport javax.swing.*;\r\n\r\npublic class TicTacToe implements ActionListener{\r\n\t\r\n\tRandom random = new Random();\r\n\tJFrame frame = new JFrame();\r\n\tJPanel title_panel = new JPanel();\r\n\tJPanel button_panel = new JPanel();\r\n\tJLabel textfield = new JLabel();\r\n\tJButton[] buttons = new JButton[9];\r\n\tboolean player1_turn;\r\n\r\n\tTicTacToe(){\r\n\t\t\r\n\t\tframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);\r\n\t\tframe.setSize(500,500);\r\n\t\tframe.getContentPane().setBackground(new Color(50,50,50));\r\n\t\tframe.setLayout(new BorderLayout());\r\n\t\tframe.setVisible(true);\r\n\t\t\r\n\t\ttextfield.setBackground(new Color(25,25,25));\r\n\t\ttextfield.setForeground(new Color(25,255,0));\r\n\t\ttextfield.setFont(new Font(\"ROBOTO\",Font.BOLD,40));\r\n\t\ttextfield.setHorizontalAlignment(JLabel.CENTER);\r\n\t\ttextfield.setText(\"Tic-Tac-Toe\");\r\n\t\ttextfield.setOpaque(true);\r\n\t\t\r\n\t\ttitle_panel.setLayout(new BorderLayout());\r\n\t\ttitle_panel.setBounds(0,0,800,100);\r\n\t\t\r\n\t\tbutton_panel.setLayout(new GridLayout(3,3));\r\n\t\tbutton_panel.setBackground(new Color(150,150,150));\r\n\t\t\r\n\t\tfor(int i=0;i<9;i++) {\r\n\t\t\tbuttons[i] = new JButton();\r\n\t\t\tbutton_panel.add(buttons[i]);\r\n\t\t\tbuttons[i].setFont(new Font(\"Open Sans\",Font.BOLD,70));\r\n\t\t\tbuttons[i].setFocusable(false);\r\n\t\t\tbuttons[i].addActionListener(this);\r\n\t\t}\r\n\t\t\r\n\t\ttitle_panel.add(textfield);\r\n\t\tframe.add(title_panel,BorderLayout.NORTH);\r\n\t\tframe.add(button_panel);\r\n\t\t\r\n\t\tfirstTurn();\r\n\t}\r\n\r\n\t@Override\r\n\tpublic void actionPerformed(ActionEvent e) {\r\n\t\t\r\n\t\tfor(int i=0;i<9;i++) {\r\n\t\t\tif(e.getSource()==buttons[i]) {\r\n\t\t\t\tif(player1_turn) {\r\n\t\t\t\t\tif(buttons[i].getText()==\"\") {\r\n\t\t\t\t\t\tbuttons[i].setForeground(new Color(255,0,0));\r\n\t\t\t\t\t\tbuttons[i].setText(\"X\");\r\n\t\t\t\t\t\tplayer1_turn=false;\r\n\t\t\t\t\t\ttextfield.setText(\"O turn\");\r\n\t\t\t\t\t\tcheck();\r\n\t\t\t\t\t}\r\n\t\t\t\t}\r\n\t\t\t\t\r\n\t\t\t\telse {\r\n\t\t\t\t\tif(buttons[i].getText()==\"\") {\r\n\t\t\t\t\t\tbuttons[i].setForeground(new Color(0,0,255));\r\n\t\t\t\t\t\tbuttons[i].setText(\"O\");\r\n\t\t\t\t\t\tplayer1_turn=true;\r\n\t\t\t\t\t\ttextfield.setText(\"X turn\");\r\n\t\t\t\t\t\tcheck();\r\n\t\t\t\t\t}\r\n\t\t\t\t}\r\n\t\t\t}\t\t\t\r\n\t\t}\r\n\t}\r\n\t\r\n\tpublic void firstTurn() {\r\n\t\t\r\n\t\ttry {\r\n\t\t\tThread.sleep(2000);\r\n\t\t} catch (InterruptedException e) {\r\n\t\t\t\/\/ TODO Auto-generated catch block\r\n\t\t\te.printStackTrace();\r\n\t\t}\r\n\t\t\r\n\t\tif(random.nextInt(2)==0) {\r\n\t\t\tplayer1_turn=true;\r\n\t\t\ttextfield.setText(\"X turn\");\r\n\t\t}\r\n\t\telse {\r\n\t\t\tplayer1_turn=false;\r\n\t\t\ttextfield.setText(\"O turn\");\r\n\t\t}\r\n\t}\r\n\t\r\n\tpublic void check() {\r\n\t\t\/\/check X win conditions\r\n\t\tif(\r\n\t\t\t\t(buttons[0].getText()==\"X\") &&\r\n\t\t\t\t(buttons[1].getText()==\"X\") &&\r\n\t\t\t\t(buttons[2].getText()==\"X\")\r\n\t\t\t\t) {\r\n\t\t\txWins(0,1,2);\r\n\t\t}\r\n\t\t\r\n\t\tif(\r\n\t\t\t\t(buttons[3].getText()==\"X\") &&\r\n\t\t\t\t(buttons[4].getText()==\"X\") &&\r\n\t\t\t\t(buttons[5].getText()==\"X\")\r\n\t\t\t\t) {\r\n\t\t\txWins(3,4,5);\r\n\t\t}\r\n\t\tif(\r\n\t\t\t\t(buttons[6].getText()==\"X\") &&\r\n\t\t\t\t(buttons[7].getText()==\"X\") &&\r\n\t\t\t\t(buttons[8].getText()==\"X\")\r\n\t\t\t\t) {\r\n\t\t\txWins(6,7,8);\r\n\t\t}\r\n\t\tif(\r\n\t\t\t\t(buttons[0].getText()==\"X\") &&\r\n\t\t\t\t(buttons[3].getText()==\"X\") &&\r\n\t\t\t\t(buttons[6].getText()==\"X\")\r\n\t\t\t\t) {\r\n\t\t\txWins(0,3,6);\r\n\t\t}\r\n\t\tif(\r\n\t\t\t\t(buttons[1].getText()==\"X\") &&\r\n\t\t\t\t(buttons[4].getText()==\"X\") &&\r\n\t\t\t\t(buttons[7].getText()==\"X\")\r\n\t\t\t\t) {\r\n\t\t\txWins(1,4,7);\r\n\t\t}\r\n\t\tif(\r\n\t\t\t\t(buttons[2].getText()==\"X\") &&\r\n\t\t\t\t(buttons[5].getText()==\"X\") &&\r\n\t\t\t\t(buttons[8].getText()==\"X\")\r\n\t\t\t\t) {\r\n\t\t\txWins(2,5,8);\r\n\t\t}\r\n\t\tif(\r\n\t\t\t\t(buttons[0].getText()==\"X\") &&\r\n\t\t\t\t(buttons[4].getText()==\"X\") &&\r\n\t\t\t\t(buttons[8].getText()==\"X\")\r\n\t\t\t\t) {\r\n\t\t\txWins(0,4,8);\r\n\t\t}\r\n\t\tif(\r\n\t\t\t\t(buttons[2].getText()==\"X\") &&\r\n\t\t\t\t(buttons[4].getText()==\"X\") &&\r\n\t\t\t\t(buttons[6].getText()==\"X\")\r\n\t\t\t\t) {\r\n\t\t\txWins(2,4,6);\r\n\t\t}\r\n\t\t\/\/check O win conditions\r\n\t\tif(\r\n\t\t\t\t(buttons[0].getText()==\"O\") &&\r\n\t\t\t\t(buttons[1].getText()==\"O\") &&\r\n\t\t\t\t(buttons[2].getText()==\"O\")\r\n\t\t\t\t) {\r\n\t\t\toWins(0,1,2);\r\n\t\t}\r\n\t\tif(\r\n\t\t\t\t(buttons[3].getText()==\"O\") &&\r\n\t\t\t\t(buttons[4].getText()==\"O\") &&\r\n\t\t\t\t(buttons[5].getText()==\"O\")\r\n\t\t\t\t) {\r\n\t\t\toWins(3,4,5);\r\n\t\t}\r\n\t\tif(\r\n\t\t\t\t(buttons[6].getText()==\"O\") &&\r\n\t\t\t\t(buttons[7].getText()==\"O\") &&\r\n\t\t\t\t(buttons[8].getText()==\"O\")\r\n\t\t\t\t) {\r\n\t\t\toWins(6,7,8);\r\n\t\t}\r\n\t\tif(\r\n\t\t\t\t(buttons[0].getText()==\"O\") &&\r\n\t\t\t\t(buttons[3].getText()==\"O\") &&\r\n\t\t\t\t(buttons[6].getText()==\"O\")\r\n\t\t\t\t) {\r\n\t\t\toWins(0,3,6);\r\n\t\t}\r\n\t\t\r\n\t\t\r\n\t\tif(\r\n\t\t\t\t(buttons[1].getText()==\"O\") &&\r\n\t\t\t\t(buttons[4].getText()==\"O\") &&\r\n\t\t\t\t(buttons[7].getText()==\"O\")\r\n\t\t\t\t) {\r\n\t\t\toWins(1,4,7);\r\n\t\t}\r\n\t\tif(\r\n\t\t\t\t(buttons[2].getText()==\"O\") &&\r\n\t\t\t\t(buttons[5].getText()==\"O\") &&\r\n\t\t\t\t(buttons[8].getText()==\"O\")\r\n\t\t\t\t) {\r\n\t\t\toWins(2,5,8);\r\n\t\t}\r\n\t\tif(\r\n\t\t\t\t(buttons[0].getText()==\"O\") &&\r\n\t\t\t\t(buttons[4].getText()==\"O\") &&\r\n\t\t\t\t(buttons[8].getText()==\"O\")\r\n\t\t\t\t) {\r\n\t\t\toWins(0,4,8);\r\n\t\t}\r\n\t\tif(\r\n\t\t\t\t(buttons[2].getText()==\"O\") &&\r\n\t\t\t\t(buttons[4].getText()==\"O\") &&\r\n\t\t\t\t(buttons[6].getText()==\"O\")\r\n\t\t\t\t) {\r\n\t\t\toWins(2,4,6);\r\n\t\t}\r\n\t}\r\n\t\r\n\tpublic void xWins(int a,int b,int c) {\r\n\t\tbuttons[a].setBackground(Color.GREEN);\r\n\t\tbuttons[b].setBackground(Color.GREEN);\r\n\t\tbuttons[c].setBackground(Color.GREEN);\r\n\t\t\r\n\t\tfor(int i=0;i<9;i++) {\r\n\t\t\tbuttons[i].setEnabled(false);\r\n\t\t}\r\n\t\ttextfield.setText(\"X wins\");\r\n\t}\r\n\tpublic void oWins(int a,int b,int c) {\r\n\t\tbuttons[a].setBackground(Color.GREEN);\r\n\t\tbuttons[b].setBackground(Color.GREEN);\r\n\t\tbuttons[c].setBackground(Color.GREEN);\r\n\t\t\r\n\t\tfor(int i=0;i<9;i++) {\r\n\t\t\tbuttons[i].setEnabled(false);\r\n\t\t}\r\n\t\ttextfield.setText(\"O wins\");\r\n\t}\r\n\r\n\r\n\r\n\r\n\tpublic static void main(String[] args) {\r\n\t\t\/\/ TODO Auto-generated method stub\r\n\t\t\r\n\t\tTicTacToe tictactoe = new TicTacToe();\r\n\r\n\t}\r\n\r\n}<\/code><\/pre>\n\n\n\n
\n\n\n\n
Tutorial MySQL<\/a>
Tutorial Python<\/a>
Object-Oriented Programming (OOP)<\/a>
Structured Programming- Advantages and Disadvantages<\/a>
Continue Statement in C Language<\/a>
Spooling and Virtual Devices<\/a>
Information Security in E-Commerce<\/a>
Client-Server Architecture<\/a>
Java (programming language)<\/a>\u2013 Wikipedia<\/td><\/tr><\/tbody><\/table><\/figure>\n","protected":false},"excerpt":{"rendered":"

Java Tic Tac Toe Game Project: We can use the PyCharm code editor for this example. If you do not know about it then follow this link-\u00a0How to install PyCharm for Python and create a program in it. Java Tic Tac Toe Game Project Sample Code: Output: Tutorial MySQLTutorial PythonObject-Oriented Programming (OOP)Structured Programming- Advantages and DisadvantagesContinue Statement in C LanguageSpooling and Virtual DevicesInformation Security in E-CommerceClient-Server ArchitectureJava (programming language)\u2013 Wikipedia<\/p>\n","protected":false},"author":1,"featured_media":103412,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[6985],"tags":[8129,8128,8127,8130],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"https:\/\/i0.wp.com\/gkscientist.com\/wp-content\/uploads\/2023\/01\/Java-Tic-Tac-Toe-Game-Project.jpg?fit=500%2C250&ssl=1","_links":{"self":[{"href":"https:\/\/gkscientist.com\/wp-json\/wp\/v2\/posts\/103410"}],"collection":[{"href":"https:\/\/gkscientist.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/gkscientist.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/gkscientist.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/gkscientist.com\/wp-json\/wp\/v2\/comments?post=103410"}],"version-history":[{"count":0,"href":"https:\/\/gkscientist.com\/wp-json\/wp\/v2\/posts\/103410\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/gkscientist.com\/wp-json\/wp\/v2\/media\/103412"}],"wp:attachment":[{"href":"https:\/\/gkscientist.com\/wp-json\/wp\/v2\/media?parent=103410"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gkscientist.com\/wp-json\/wp\/v2\/categories?post=103410"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gkscientist.com\/wp-json\/wp\/v2\/tags?post=103410"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}