Java tic tac toe 2 player gui

The following is another homework assignment which was presented in an intro to Java class. This program was used to provide more practice creating a graphical user interface. This program features a graphical user interface GUI which has nine buttons arranged in a square.

A user plays the game by clicking the mouse in a available button. This action will clear away any text on the 9 game buttons. The 9 game buttons are still disabled, but the radio button is enabled. When there is such a winner, the symbol of the winner is displayed to the screen.

Tic Tac Toe game : Simple Gui

Sometimes all 9 game buttons have been clicked on but there is still no winner. This program was implemented into 3 different files, so the code for this program will be broken up into 3 sections.

Another file contains the implementation of the graphical user interface GUIand the last file contains logic, which in this case is the game algorithms. This is the GUI graphical user interface class for a three-file solution which implements the game of tic tac toe. The sole purpose of this source code is to define the GUI and call methods in the BusinessLogic class when needed.

This is the BusinessLogic class for a three-file solution which implements the game of tic tac toe. The is the class containing algorithms. The code is heavily commented, so no further insight is necessary. If you have any questions, feel free to leave a comment below. This program uses custom fonts. To obtain those fonts, click here! The driver program. BOLD,18 ; mnuMain. NORTH ; window. BOLD. BOLD18. ShowGame pnlSouthpnlPlayingField.

NORTH. GetMove currentMoveremainingMovesfont. CheckWin. RedrawGameBoard. Start of all the other methods. The way this checks the if someone won is:. Secong: it checks if btnEmpty[x] is equal to btnEmpty[y]- x being winCombo[each set][the first number].

Third: it checks if btnEmtpy[y] is eual to btnEmpty[z]- y being the same y as last time and z being. Conclusion: So basically it checks if it is equal to the btnEmpty is equal to each set of numbers. The is the class. String startingPlayer. Leave a Reply Cancel reply. All Rights Reserved. Powered by WordPress and FlexiThemes.By using our site, you acknowledge that you have read and understand our Cookie PolicyPrivacy Policyand our Terms of Service.

Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It only takes a minute to sign up. I had never done a full interactive app before so this is a really big step for me. It's very simple and fits into a single file. One thing that will be odd is the fact that I didn't use arrays. I don't know why but there were a ton of runtime errors occuring whenever I tried to do something that involved an object in an array. The first, as you pointed out yourself, is that you don't use a Tile[][] for the tiles.

The second is the fact that you don't use any encapsulation. And to make things worse, your Tile class is doing all the work, mostly by accessing the variables of your TicTacToe class. Having everything in one file is not something to be proud of. At best, it's something that we can tolerate if the program is truly small enough to just be 2 methods or so.

For your tictactoe game it really should be split up into more manageable classes, each into their own file. Let's first look at what we expect to find in a Tile class. A Tile should represent a single square. So it should have a text that can be either empty, X or O. It can also have a border and a color. It shouldn't even have access to other Tiles at all. The only useful method that it could have that uses another Tile, is a helper method to check if it has the same text as that other tile.

Notice how I made the text field private? No other class should even know that the tile stores this as a text field. We might have stored it into a String for all they care.

As long as we have a method getText that returns if it's an XO or neither. Now that we fixed the Tile class let's look at all the things that should be improved in the TicTacToe class. First of all, let's encapsulate all the variables. This is as easy as adding the private keyword to all of them:. Next a method that overrides a method of a superclass should be anotated with Override.

Now that my IDE stops complaining we can address the biggest issue of this class. Putting all the Tiles into an array. That didn't look too hard, did it? We just loop over the rows, and then over each tile in a row so handle all the tiles.

For the creation of the tiles we have to put in a bit more effort. Not only do we need to alternate the colors, we also need to add a clickListener because we removed that from the constructor of the tile. I would write the construction of all the tiles like this:. Notice how I put the checking what needs to happen into it's own method.

We do need to pass the tile to that method so that it knows which tile was clicked. Now let's look at what that method has to do.By using our site, you acknowledge that you have read and understand our Cookie PolicyPrivacy Policyand our Terms of Service.

The dark mode beta is finally here. Change your preferences any time. Stack Overflow for Teams is a private, secure spot for you and your coworkers to find and share information. So I am making a tic tac toe game with user friendly GUI interface. I have almost completed the game but there seems to be a problem with my checkWin function. Up to this point, everything was working just fine until I added the checkWin function and the parameters associated it with it.

Before I implemented the checkWin function, you would press a button, it would be marked X and then there would be an automated computer move that would place an O randomly on the board where there isn't an X. Code is posted below. When I run the code, and you press a button, the applet just freezes and you can't even exit out of it without end task, ctrl-alt-del Again, everything worked fine up until the checkWin function and now I'm facing a run-time error.

The loop in you checkWon can't end while the game is I complete, but you can complete the game because the while loop is blocking it. The issue seems to be that the AI "compTurn " Is running an indefinite loop, and never gives the player an actual turn. It instead constantly tries to do its turn. To combat this, add a regulating variable I added "int whoseTurn" in the example fix below. That only combats the runtime error, though.

Another issue is that on MacOS devices I tested it on MacOS X the buttons are drawn before the backbuffer, and so the result is a blank backbuffer and a unplayable game. There is yet another issue, whereas the "Start button is completely useless, and does not affect the game in any way.

I removed that in the example code below. The final issue is that your "checkWin" mehtod is simple, but oftentimes wrong. I have replaced it with a longer, more accurate win checking system. Learn more. Asked 6 years, 2 months ago. Active 2 days ago. Viewed 31k times. SOUTH ; menu. EAST ; start.For more java articles ,Click here to Subscribe JavaHungry.

Java Hungry. Java developers tutorials and coding. Before discussing tic tac toe java codewe should learn about the rules and regulations first.

This game is played between two players. The game has simple 3 rows and 3 columnsso make it a 3x3 grid. If players press the exit button then the frame closes. Board is drawn first using drawLine and drawOval methods.

java tic tac toe 2 player gui

Player X and Player O click the mouse on the empty grid of the board 3. If Clear then the board is again drawn and ready to play again. If exit then the frame or window exits from the screen. This is the second post for the Tic tac toe game. In this we added two functionality clear and exit button to the window.

If the players press the clear button then the frame is redrawn to play the game again.

java tic tac toe 2 player gui

Applet ; import java. BasicStroke ; import java. Color ; import java. Container ; import java. Graphics ; import java. Graphics2D ; import java. ActionEvent ; import java. ActionListener ; import java. KeyEvent ; import java. MouseEvent ; import java.

MouseListener ; import java. WindowEvent ; import java. WindowListener ; import java. PrintStream ; import javax. JButton ; import javax.By using our site, you acknowledge that you have read and understand our Cookie PolicyPrivacy Policyand our Terms of Service. Code Review Stack Exchange is a question and answer site for peer programmer code reviews.

It only takes a minute to sign up. Timer in your initiation. And then put the final X in the center. Can you play a second game?

java tic tac toe 2 player gui

There is no way to reset count for a new game. It would continue to count above 9 with additional moves! Can you play two games at once? There is only one board object. Why are these variables static?

It prevents multiple TicTacToe objects from being created. If you removed static from the variables, you could start a new game by creating a new TicTacToe object and resetting the GUI.

Advanced: Also, you could allow the computer to experiment with different moves, and look several moves in the future, Use an else clause:. Who can win on any turn? The player that just made a move, of course. And their symbol is stored in string a terrible variable name, by the way. So if the above test was to pass, all the symbols would equal stringso you could write it a little more concisely as:. Sign up to join this community.

Tic Tac Toe In Java By Using NetBeans (logics with easy explanation)(part 1)

The best answers are voted up and rise to the top. Home Questions Tags Users Unanswered. Asked 3 months ago.

Subscribe to RSS

Active 3 months ago. Viewed times.The GUI should resemble the one shown here. The game play is simple. The human player plays first by clicking any of the squares. The square clicked by the human displays a large X. After the human plays, the program determines if the human has won or forced a draw.

If so, a message is displayed, the board is reset, and a new game begins. If not, the computer chooses a move and marks its square with a large O. The program then determines if the computer has won the game. If so, the program displays a message, resets the board, and starts a new game. If not, the human player plays again. This continues until one player wins or all of the squares are filled. In other words, it must implement the exact same methods.

For your convenience, these methods are repeated in the following table. Throws IllegalArgumentException if square is not one of the allowable values, player is not 1 or 2, or the specified square is not empty. Determines whether the game is over. Returns 0 if the game is not over, 1 if X has won the game, 2 if O has won the game, and 3 if the game is a draw.

Returns an integer representing the next move for the computer opponent. Returns a string that represents the current status of the board. The string includes new-line characters to display the rows as well as separator lines on separate console lines, as in this example: O O — — — X — — — X.Rebecca Berry wrote: I went ahead and put a System.

Now when I run the program this is what I get when I click on the spaces. I clicked top row to bottom row, left to right run: 00 01 02 10 11 12 20 21 Forum: Beginning Java. Rebecca Berry. Hello again! When I run it the board comes up like it should, however, nothing happens when I click on a space to make a move. When I click on the space it should mark it with a 'X' and then state it is O's turn and so on. What am I missing?

Thanks as always for the pointers. JFrame; import javax. JButton; import javax. JPanel; import javax. JLabel; import java. ActionListener; import java. ActionEvent; import java. BorderLayout; import java. GridLayout; import java. Norm Radder. How are you debugging the code? You need to see what is in s on line to be sure the logic that follows is right.

Add a print statement to see. I went ahead and put a System. Campbell Ritchie. Why are you using addActionListener this?

Create or Make Simple Tic tac toe Swing based Gui Java program Source code

It is easy to buy books which teach bad programming and unfortunately beginners don't know they are being taught badly. You have some confusing features. Your indentation is still inconsistent, so it is difficult to see that line 60 is the beginning of a new method.

That method uses a parameter board which shadows a field of the same class. That can cause confusion, too. Can you run the game without the GJI?

It is usually bad design to put anything inside a GUI component class except displays. If you do things to their objects, they call methods of other objects listeners and those listeners actuate the logic of the actual app.

Knute Snortum.


thoughts on “Java tic tac toe 2 player gui”

Leave a Reply

Your email address will not be published. Required fields are marked *