Your task is to develop a Tic-Tac-Toe (also called as “Noughts and Crosses”) game in Java using Swing. In this assignment, you are asked to implement a single-player version of the Tic-Tac-Toe game with easy-to-use graphical user interface (GUI). One player will be the user and the second player will be artificial intelligence (AI), namely, the computer. The entire procedure of the implementation has been split into three tasks with detailed description and explanation provided. By completing the three tasks, you will develop your own Tic-Tac-Toe game. Task 1: GUI Implementation Please implement a GUI satisfying the following requirements. • A menu bar with two buttons “Reset” and “Quit”. User can click the “Reset” button to restore the software to the initial status. User can software the game by clicking the “Quit” button; • The top pane is an information board to display username and present some basic guidance to the user about this game. The information board will also print out the winner (user or AI) at the end of the current game session; • The bottom pane is a text box, where user is required to enter a username before starting the first game session. Once the username has been given, it cannot be changed anymore unless the entire game software is reset by clicking the “Reset” button from the menu; • The right pane has two buttons “Start” and “Re-play”. User can click the “Start” button to start the game session if a valid username is given. During the middle of a game session, or the current game session is finished, user can click “Re-play” to start a new game session (without changing username). Note that the “Start” button will be deactivated after the game starts, and the “Re-play” button will be activated only after the first nought has been placed by user; • The central area is a three-by-three square grid with a total of nine buttons. During a game session, user can click one of the buttons to place a nought, namely, mark “O”, to the selected cell within the grid. After this, AI will place randomly a cross, namely, mark “X”, at any cell within the grid if it is not occupied. Then, user can place the next nought at an unoccupied cell by clicking the corresponding button, and the cycle continues until all nine cells have been occupied by noughts and crosses. However, once a particular cell has been occupied by either a nought or a cross, user and AI cannot overwrite that mark. In other words, if user has already clicked the button to place a nought or AI has placed a cross on the cell, clicking the same button will do nothing, and AI cannot change the mark on it as well. Note that you could change the layout of your GUI as you prefer. However, the GUI should contain all the elements as described above and the functionalities should be kept the same. Importantly, the GUI must be easy-to-use. Tips: (a) JMenu - menu; (b) JLabel - information board; (c) JTextField - text box; (d) JButton - buttons. Task 2: Adding Some Intelligence to AI In the GUI you have implemented in Task 1, AI will place a cross at an unoccupied cell within the grid in a purely random manner. In Task 2, you are asked to implement a basic algorithm to let AI to act in a more clever (semi-random) manner such that AI will prefer the empty cell that is surrounded by the most cells with noughts. In other words, the empty cell with the most noughts nearby will be more likely to be chosen by AI for placing a cross. Task 3: Finding the Winner Once the AI has been improved in Task 2, you will realise that the GUI will continue the current game session until all the nine cells within the three-by-three grid have been filled. However, the problem is that the current game session may already finish before that, i.e., when user has placed three noughts or AI has placed three crosses in a row. Therefore, in Task 3, you are asked to implement an algorithm such that the software will check whether one of the two players has actually won the game after the fifth mark has been placed onto the grid. Once the algorithm identifies the winner, the current game session finishes, and user and AI will not be able to put any marks further. The information board will print out who is the winner. After that, user can either click “Re-play” button to restart the game session or use “Re-set” option on the menu to initialise the software. Note that in some cases, there will be no winner even after all nine cells have been occupied. The algorithm needs to identify such cases and the information board needs to display the message accordingly.
Your task is to develop a Tic-Tac-Toe (also called as “Noughts and Crosses”) game in Java using Swing.
In this assignment, you are asked to implement a single-player version of the Tic-Tac-Toe game with easy-to-use graphical user interface (GUI). One player will be the user and the second player will be
Task 1: GUI Implementation
Please implement a GUI satisfying the following requirements.
• A menu bar with two buttons “Reset” and “Quit”. User can click the “Reset” button to restore the software to the initial status. User can software the game by clicking the “Quit” button;
• The top pane is an information board to display username and present some basic guidance to the user about this game. The information board will also print out the winner (user or AI) at the end of the current game session;
• The bottom pane is a text box, where user is required to enter a username before starting the first game session. Once the username has been given, it cannot be changed anymore unless the entire game software is reset by clicking the “Reset” button from the menu;
• The right pane has two buttons “Start” and “Re-play”. User can click the “Start” button to start the game session if a valid username is given. During the middle of a game session, or the current game session is finished, user can click “Re-play” to start a new game session (without changing username). Note that the “Start” button will be deactivated after the game starts, and the “Re-play” button will be activated only after the first nought has been placed by user;
• The central area is a three-by-three square grid with a total of nine buttons. During a game session, user can click one of the buttons to place a nought, namely, mark “O”, to the selected cell within the grid. After this, AI will place randomly a cross, namely, mark “X”, at any cell within the grid if it is not occupied. Then, user can place the next nought at an unoccupied cell by clicking the corresponding button, and the cycle continues until all nine cells have been occupied by noughts and crosses. However, once a particular cell has been occupied by either a nought or a cross, user and AI cannot overwrite that mark. In other words, if user has already clicked the button to place a nought or AI has placed a cross on the cell, clicking the same button will do nothing, and AI cannot change the mark on it as well.
Note that you could change the layout of your GUI as you prefer. However, the GUI should contain all the elements as described above and the functionalities should be kept the same. Importantly, the GUI must be easy-to-use.
Tips: (a) JMenu - menu; (b) JLabel - information board; (c) JTextField - text box; (d) JButton - buttons.
Task 2: Adding Some Intelligence to AI
In the GUI you have implemented in Task 1, AI will place a cross at an unoccupied cell within the grid in a purely random manner. In Task 2, you are asked to implement a basic
Task 3: Finding the Winner
Once the AI has been improved in Task 2, you will realise that the GUI will continue the current game session until all the nine cells within the three-by-three grid have been filled. However, the problem is that the current game session may already finish before that, i.e., when user has placed three noughts or AI has placed three crosses in a row. Therefore, in Task 3, you are asked to implement an algorithm such that the software will check whether one of the two players has actually won the game after the fifth mark has been placed onto the grid. Once the algorithm identifies the winner, the current game session finishes, and user and AI will not be able to put any marks further. The information board will print out who is the winner. After that, user can either click “Re-play” button to restart the game session or use “Re-set” option on the menu to initialise the software. Note that in some cases, there will be no winner even after all nine cells have been occupied. The algorithm needs to identify such cases and the information board needs to display the message accordingly.
Trending now
This is a popular solution!
Step by step
Solved in 4 steps with 2 images