banner



Object Oriented Design For Game Tic Tac Toe

Let's Make Tic-Tac-Toe!

Bradley Haley

A very easy Tic-Tac-Toe game!

We will be making a super simple Tic-Tac-Toe game! This 2 player pencil and paper game has very old origins and is ubiquitous across many cultures. This version will be made with Java and I will be using the IntelliJ IDE. This game will also feature a SUPER easy computer that just places pieces randomly. In the future blog post, we will talk about how to make it unbeatable using a minimax algorithm but for now we just need to make something show up on the screen and make sure we know when the game is won. Follow the pictures and descriptions for each step and let's start coding!

Lets first decide how we want to get our project concerns separate. Since Java is an object oriented language we need to think about our game in terms of OOP. You may want to separate your files differently so fell free to do so. In Main.Java is were I will be prompting the questions but I will need to refer to a move done by a user and by a computer. The game logic will be held in a different file as well. Once you have decided how to separate your concerns lets start programming! Since our game is super simple let's let the user always go first and will always be X. The computer will follow suit and be O.

Step 1: Setup

I'm going to let IntelliJ do all the hard work for me so just create a new project in the directory you want and call it TicTacToe. Let's also make three Java class files. One for the game logic called Game.java, one called Computer.java for a simple computer player and a final TicTacToe.java where we will prompt the user for questions. Here is my file structure!

ItelliJ took care of most of the work for us!

Step 2: The Board and Game

We will make our board and all the game logic in Game.java. The board will be a 2D character array demonstrated below. I'm also going to keep track of the positions with another array called placements. There are 8 winning positions so I create a function that checks those positions in the placements array and I call that after every move. I also added getter functions as well.

My functions so far.

I also create a function called show options that will show all of the options and print board that prints the board. It's amazing how simple it can be to name functions. These functions are nested loops that iterate over the 2D array.

These last ones are going to be functions that place a position for the computer or the player. the positions are numbered 1–9 for the user so I subtract one to make it match the placements array more easily. It takes a character X or O along with a position. We need to check if that position is already taken so let's also create a function that checks if that position is taken and will return a boolean.

My functions

Finally we need functions to check if the game is a tie, the last move has been made or the game is won. We have done all of the hard work so these are simple functions.

My Code…

… is all over…

… the place!

That should wrap it up for the board and game logic. This is the most intensive part so nice job!

Step 3: The Computer

All our computer is going to be doing is placing random pieces since that's our minimum viable product(MVP for short). I need to get the placements array and choose a random number. If that random number is taken then choose another until it isn't. Once it has found a move all it needs to do it just place it! It's really that simple! We can make it more difficult using the mini max algorithm but that's a separate topic.

Really that simple!

Step 4: The User

The last step in this is to just put it all together! We first need to bring all these components together by instantiating them and make a nice little intro function!

Our instantiation and intro function!

Calling our show options method

Looks pretty good!

After the prompt we need to take a user move. We need to validate this input to make sure it's a number 1–9. I use a try catch block to check if it's not an integer and if it is the program exits. This is wrapped in a while loop to check if the number is less than 1 or greater than 9 and if it is it will prompt them to pick another number and FINALLY if the position is taken they have to choose again! That's a lot of validation! The computer has his own little function since we programmed it to make only choose correct choices.

A little complicated but does what it needs to do!

A much simpler function!

Now this part is just for me since I enjoy breaking out into tinier functions but I made functions for print information if the game is tied or the game is won but after that all we need to do is wrap it all up. The way I designed it was to let the user be X and always go first. I check to see if the game is won after every move which is slightly overkill but it's our MVP! Now since our there are 9 positions 2 players the first player will have the last move. You can fall into an infinite loop here if your not careful. If you check for a tie game at the end of both moves and you place your move then the computer goes which keeps looking for a piece but there aren't any! Again it's over kill but I check to see if there are any tie games after the users every turn. Once we have that we have a fully functioning TicTacToe game! Nice job! In a later blog post we will make an unbeatable computer using the mini max algorithm!

Our last method that wraps it all up!

Object Oriented Design For Game Tic Tac Toe

Source: https://levelup.gitconnected.com/lets-make-tic-tac-toe-51dd3fd188d

Posted by: dyerlaceirdid.blogspot.com

Related Posts

0 Response to "Object Oriented Design For Game Tic Tac Toe"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel