Java tic tac toe game โญ•

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
how's it going everybody it's bro here hope you're doing well and in this video I'm going to teach you guys how we can create a game of tic-tac-toe using Java so sit back relax and enjoy the show if you find this video helpful please remember to Like comment and subscribe your support will help keep this channel running all right everybody let's create a game of tic-tac-toe so I'm going to go to file new Java project to create a new project I'll call this tic-tac-toe and click finish don't create a module then within this project folder I'm going to create a new class and I will call this class min then check this public static void main checkbox and then we're going to create another class so new class and I'll call this class tic-tac-toe and click finish ok let's create an instance of tic-tac-toe within our main class so within the main class I'm just going to get rid of this comment because we don't necessarily need this let's create an instance of our tic-tac-toe clasts we're going to type in the name of the class tic-tac-toe we'll need a name for this instance I'll just call this tic-tac-toe all the lower case but it doesn't really matter what you call it equals new tic-tac-toe I'm gonna get real sick of saying tic-tac-toe by the end of this video just so you know alright so that's everything we need for this main class let's head to our tic-tac-toe class we'll need a few imports before we begin working on this class so make sure you have at least these four imports and now next to this class definition we'll want to implement the action listener interface so right after tic-tac-toe we're going to type in implements action listener and currently this is going to be underlined red that's fine we're going to fix this in just a moment because we're implementing an interface so if there's any declared methods we need to utilize them within our program so let's create the constructor for this class so I'm just going type in the name of the class followed by a set of parentheses in curly braces this is the constructor we'll just leave this empty for now and fill this in later so we'll also need to add unimplemented methods so we need to utilize this actionperformed method since we're utilizing an action listener interface next we'll create a method that will determine whose turn is first and this will be random will it be X's turn first or au turn so let's create a public void method called maybe first turn and we'll just declare this but not actually fill this in quite yet and next let's create a method that will check to see if there are any winning conditions or if any player has currently won so we'll do the same thing public void check and now what we're going to do is create two methods one if X wins and the other if a wins and we're going to check to see who wins within this check method and at the end we'll just call one of these win methods basically so we'll create one for X first so public void X wins and we'll set up some parameters so we're going to receive the winning combination of buttons basically so what buttons did the user press to actually win this game and we're actually going to color these green to display the winning combination basically so we'll just set this up to be integer values so we'll set up these parameters with int a int B and int C and that should be good and let's do the same thing for oh but we can just copy this and paste it and change X to oh okay so that should be good these are all the methods that we need so let's just double check this we have the constructor for tic-tac-toe we have our actionperformed method the first turn method the check method X wins and Owens so let's start before the constructor and declare a few things now with this program we're going to determine whose turn it is first randomly so we're going to create an instance of the random class to help us with that so random and we'll call this random equals new random now we'll need AJ frame so J frame and we can call this frame equals new J frame and let's create a panel to hold the title so this will be a J panel and we'll call this title underscore panel equals new J panel and let's create another panel to hold all of the buttons that we have so J panel and we'll call this button underscore panel equals new J panel and we'll want a label to serve as maybe a text field to display a message of some sort so J label and let's call this text field equals new J label all right we're going to want an array of J buttons so we're going to type in J button then straight braces because this is going to be an array and we'll call this buttons equals new J button straight braces and we actually need nine buttons so within the straight braces we can just type in nine because we don't need any more or less than nine for this game and lastly we're going to create a boolean variable called player one underscore turn so this is going to be true if it's player ones turn or false if it's player two's turn so it's not really necessary to have a boolean for player two's turn because one's enough if player one is false then it's player two's turn basically and vice versa so that should be everything we need to declare before we move on to the constructor for the tic-tac-toe game so within the constructor let's start with creating the jframe first so we're going to type in frame dot set default close operation and then within the parentheses J frame dot exit close then we're going to set a size for this frame so frame dot set size and I think 800 by 800 would be good for the size for this frame okay and then what we're going to do is type in frame dot get content pane and then we're going to set the background color so set background then we're going to type in new color and then we can type in some RGB values so I'm going to select something that's darker like 50 50 50 so this is a lighter shade of black otherwise if you want something generic you can just type in like color dot I don't know red but otherwise you can be more specific and place some RGB values here okay and then we're going to set a layout for this frame so frame dot set layout and I usually type in null but what I'm going to do is type in new boarder layout and then lastly we want to set the frames visibility to true so frame dot set visible true and then we're good with the frame so let's move on to our text field so after the frame we're going to maybe first set the background color for our text field so text field dot set background and then we can pick a color so new color and this is going to be the same process as what we did up here so I'm just going to pick maybe 25 25 and 25 for RGB values and we can set the text color so what I'm going to do is type in text field dot set for ground and then new color and I think I will pick the color of maybe green so maybe 25 to 5 five and zero okay and let's set the font so text field dot set font new font so I'm going to pick ink free this time so that's the font style and maybe I'll have this be bold so font bold and I think 75 would be good for a font size okay and then I'm going to set the horizontal alignment so text field dot set or is Szanto alignment J label dot Center so remember we're not using no layout manager we're using a border layout for this okay then let's set some text so text field dot set text and I'll just display the title of the game for now so tic-tac-toe and what I'm going to do is type in text field dot set opaque to true so this is everything we need for the text field we're going to add this text field to a title panel and then eventually we're going to add this title panel to our frame so let's work on the title panel next so we're going to type in title underscore panel dot set layout and we're going to use a boarder layout so new boarder layout and then for the next line we can set the balance for this panel so I'm going to type in title panel dot set bounds and we're going to place the coordinates of where we want this to start so if we want to start in the top left corner that is where x is 0 Y is 0 and for the length maybe I will select 800 to match the size of this frame and then for a height maybe 100 that should be good okay so then let's add our text field to our title panel and then add the title panel to our frame so we're going to type in at the bottom title underscore panel dot add text field and then we're going to type in frame dot add title underscore panel and then let's run this and take a look okay so what we have here is that this title panel is taking up the entire frame that we have so since we're using a boarder layout we can specify a border that this title panel can stick to basically so if we want this title panel to stick to the top when we add the title panel to our frame what we can type in after this is comma then boarder layout dot north and when we rerun this our title panel is going to stick to the top of our frame and it's actually adjustable too if we were to resize the frame all right so let's work on the button panel next so after the title panel we're going to type in button underscore panel and we're going to use a different layout a grid layout so we're going to set layout and then within the parentheses new grid layout and we want this to be 3 by 3 since we have nine buttons and let's also change the background color and honestly I just feel like copying this and pasting it because I'm too lazy to type stuff out and I'm going to change this to button panel and maybe we'll change this to 150 150 150 it doesn't matter we're going to be adding buttons to this so you can't see the color eventually I just want to be sure that this panel is added so I'm just going to make the color something drastically different all right then we need to add the button panel to our frame so I'm going to type that after we add the title panel so frame dot add then we're going to add the button underscore panel and let's take a look okay that should be good and this is going to be our button panel and the reason that we colored it is just a test to see if it's actually appearing so that should be good next let's add all of the buttons so let's work on these buttons before we add everything to our frame and since we're dealing with an array of J buttons we can easily use a for loop to take care of buttons one through nine for us so let's begin by creating a for loop so we'll create an index int I equals zero and we'll continue this as long as I is less than nine one for each button and then increment I by one each time and then within this for loop we have a few lines to fill in so we're going to take our buttons array at index I and we're going to create a new J button and then for the second line we're going to add this specific button that we're working on to our button panel so let's I pin button underscore panel dot add buttons at our current index and then for the next line let's set the font so buttons at index I and we will type in dot set font and then within here we can specify a new font and for the font style I will pick M the boli but pick a font that you like and then after this we can specify if we want this like bold or italic or anything but I'll set this to be bold I think and then a font size and I will pick 120 all right and then we do not want these buttons to be focusable so we're going to take buttons at index I and we're going to set focusable to false and lastly we just need to add an action listener to each of these buttons so buttons at index I dot add action listener then within parentheses type in this all right so let's run this and we get a nice grid of buttons and these are actually resizable too so that's kind of cool so now let's determine to see whose turn it is first so we'll have the speed random at the start of your game so at the end of the constructor let's call the first turn method so first turn and then fill in this method next all right so there's a few different ways we can do this I'm thinking what we can do is just have the random class pick a random integer between maybe 0 and 1 if it's 0 it'll be player 1 Stern if it's 1 then it's player two's turn so we can type in if then random dot next int and we want two numbers so we're going to type in 2 here so we'll get a random number either 0 or 1 so if this number is equal to 0 what we'll do is that we're going to take this boolean value player 1 turn and we're going to set this to true so player 1 turn equals true and then let's also change the text for the text field to reflect player ones turn so one is X so we're going to take text field dot set text then within parentheses we'll just type in X turn alright then we'll need an else statement so if it's not player ones turn it must be player two's turn so we can just copy this and we'll change true to false and text field dot set text to 0 or oh and let's try this now to see if it works all right so it's always turn let's try it again and it is now X's turn however um if you want to have that title display for a little bit before assigning a turn for somebody we can easily just have this program sleep for a little bit so what I'm going to do when we start first term I'm just going to take our thread and have this sleep for maybe 2,000 and then we need to surround this with a try and catch block all right so let's try this now so it says tic-tac-toe for two seconds and then it assigns a turn to either X or o randomly so this isn't entirely necessary but if you want to add a delay before assigning somebody's turn you can just add this here so that's everything we need for our first turn method so let's head to our actionperformed method so the first thing that we're going to do is that we're going to stick everything within a for loop so this is going to run nine times whenever a button is clicked so for int I is equal to zero and then we will perform this as long as I is less than nine and then we will increment our counter by one so we're basically going to be checking each of our nine buttons so the first thing we'll do is if e dot get source is equal to buttons at our specified index of our for loop then what we'll do is that we will check to see whose turn it is so we'll need an if-else statement so with this line I'm going to type in if player one underscore turn and we don't need to say equals true because this is already a boolean value so if it's player one Stern what we're going to do is that we're going to check to see if there is any text assigned to this button because if somebody clicks on a button we're going to change the text on that button to reflect either an X or a o but let's check to see if that button has some text first so if this specific button buttons at index I dot get text function is equal to and then we'll just place a blank set of double quotes here so what would I've been within this if statement is buttons at index I we're going to maybe set the foreground color set for ground and then we'll pick a new color so this is the color for X maybe I'll pick blue so that is to know that's red actually 255 0 0 all right and then let's also set the text so buttons I set text to equal just a giant X that should be good and then we're going to flip player once turn so player 1 underscore turn equals pulse and then for the next line we're going to change the text field to display that it is now o Stern so text field dot set text to Oh turn now let's do the same thing for player 2 who is o so let's create an else statement right where this ends so it looks like it ends right here so we'll create an else statement and we're going to copy all this and paste it within here and we're going to change a few things so let's have player X be read and player o be blue so I'm going to change this value to 0 and this one to 255 for blue set text to a giant o player 1 turn will be set to true and then set text for the text field to X turn okay and then the last thing that we should do is call our check function so let's add that the end of these if-else statements so we'll call the check function but make sure you spell it right okay let's test this out then so it is o's turn first and if we were to click one of these buttons this is now oh let's try it again with X and this is now X this is pretty sweet all right so we'll want to check to see if we have any winning combinations so that's what we're going to work on next within our check method or I guess I'll be honest with you this part of the project is going to be kind of a pain in the butt to code so what we actually need to do is check to see if each of these winning combinations has text that all matches but we're going to do a lot of copying and pasting though so it shouldn't be too bad so first let's check to see if this first row has any matches and then we'll do some copying and pasting right after that so we're going to check using an if statement and then add a set of curly braces after and then let's add another set of inner parentheses let's strike to see if buttons at index zero that is our very first button in the top left corner is equal to X so we need to use the get text method and check to see if this is equal to the character capital X and then we're going to add the and logical operator and I'm just going to copy this and paste this and we are going to change this next part to buttons at index one but let me format this a little bit so it's even organized okay and then let's do the same thing for buttons at index two and then we can get rid of this last one okay so that shouldn't be too bad so what we're going to do if this first row has all matching X's what we're going to do is call the X wins method and then we need to send in some arguments so these are three integers so within the curly braces here what we can type in is we will call the wins method and send in three integers zero one and two because that's the winning combination basically okay so let's do the same thing for the other winning combinations and I'll walk you through it and we're just going to copy this and paste it because I really don't want to do this by hand so we'll check three four and five and send these in as well then we're going to check six seven and eight six seven eight and then zero three six okay then we got one four and seven no not 72 7 1 4 7 2 5 8 zero for it and then lastly two four six and then we need to do the same thing for oh but we're just going to copy all this and change just a few things so we'll copy all of this and paste this right underneath and we're going to change all X's to O's and actually guys there's a shortcut that'll let us change all capital X's to capital O's so what I'm going to do is highlight all of this text then this is for the Eclipse IDE but if you use them like IntelliJ IDEA I'm sure there's something similar setup so highlight all of the text that you want to look through go to edit find replace and I'm going to replace all X's with capital O's and then make sure the case sensitivity is on then I'm going to replace all and now all of these capital X's with in the oh wind conditions is now capital o and we have to do the same thing for lowercase X however we do have a lowercase X within get text so I kind of want to avoid doing that so I'm just going to change these by hand real quick and change X to lowercase o so this will only take one moment so once you change all X wins to owens within the o wins condition section we can move on to the last few steps of this project so let's fill in a few lines within the X wins method so we need to indicate that the player X has won so one thing that we can do is actually change perhaps the background color of the winning combination of buttons so let's type in buttons at index a and this is going to be passed in as an argument and we're going to set background to maybe color dot green and let's do the same thing for B and C so buttons at index B and buttons at index C and we'll want to disable all buttons so people can't keep playing so we'll put this within a four and we'll do this for all nine buttons so int I equals zero will do this nine times so I is less than nine and increment I by one and then we're going to take our buttons at index I set enabled false and then lastly we're going to change the text field to X wins so text field set text X wins and then let's copy this and do the same thing for o then we are just going to change set text to oh and that's it that should be this program so let's actually play this now all right here is our game it is X's turn I'm just going to start making up stuff and here it is the winning combination X wins all right well that's everything so if you would like a copy of all this code I'll post this in the comments down below but yeah that's how you can create a game of tic-tac-toe using Java hey you yeah I'm talking to you if you learn something new then you can help me help you in three easy steps by smashing that like button drop a comment down below and subscribe if you'd like to become a fellow grow
Info
Channel: Bro Code
Views: 74,253
Rating: undefined out of 5
Keywords: tic tac toe program in java, tic tac toe game in java, tic tac toe in java, tic tac toe java, tic tac toe program, how to code tic tac toe in java, tic tac toe in java code, java tic tac toe, java tic tac toe tutorial
Id: rA7tfvpkw0I
Channel Id: undefined
Length: 30min 0sec (1800 seconds)
Published: Sun Jul 05 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.