Chris, as a fellow programmer, thanks for all the time and effort you put into this project. I learned a lot from your videos about Windows application development. Keep up the great work!
Great tutorial to follow as a complete beginner. Had watched quite a few videos of C# for beginners, so thought i'd move onto a game. This was greatly structured and easy to follow and learn along
Chris, Your patience is beyond me. Not only do you show how to build this Tic Tac Toe program you take the time to cater to your commenters (i.e.3a and 4). I'm a really old guy (50+) and just decided to go get my BS in IT to keep my mind engaged. Your tutoring methods are so easy to follow and you explain stuff as well. Thanks for making these videos. I do have a question for you though. Once you create this game how do you get it out of Visual Studio and onto any PC? Like a packaged program I guess.
D Henson , Thanks for the kind words. With these really simple projects (like most of mine are), you can just go into the bin/debug folder of your project (after you have run it - i.e., the play button), and you'll see an executable (exe file). You can just double click to run the application. If you were looking to actually distribute what you build in a more professional way, you could add Install Shield's limited edition to Visual Studio, but it's a lot more involved. Not necessarily difficult, but takes some effort and a decent amount of trial and error to get it right. :)
Chris, i will say Thank you so much, because your tutorials help me to make my assignment :)) ill wait your new tutorial how to make another simple game with AI
Hi,Chris, I have some questions. Your videos about Tic Tac Toe really helps me, but I got an error in the computer_make_move method where I was getting an unhandled null reference exception, method disableButtons doesn't working. When i finish the game, empty buttons stay active and I can click on them. How can I solve this problem? Please,help me(
I know this comment is 4 years old, but maybe someone else is wondering about the disableButtons method not working. You need to move the try catch in the disableButtons method inside the foreach, just like Chris did with the "New Game" menu item click event in part 2.
@@scy216 there’s another way to do it. Again I know this comment is 2 years old, but I was making a program and I got stuck on that same part. For about 4 hours, until I finally found another solution foreach ( Control c in this.Controls ) { TextBox tb = c as TextBox; if ( null != tb ) { } } I then switched text box to button and it worked.
Great tutorial. But going for the corner square when you are player 2 is suicide, the only way player 1 doesn't win is if player 2 plays middle square on turn 2. You should only go for the corners if you are player 1. Assuming both players are playing the optimal way of course.
Would you be able to show how one might be able to display the game board or past games once called upon? for example, file, load game history, ( and in this new page you could click on game 1, game 2 etc.. and it would show the outcome of each game selected?
I've got a little problem with the against_the_computer and There_Is_a_winner system. When player against the computer if player wins the computer make a move and is also declared a winner. Is there a way to lock out the computer when the winner is announce? Tried a couple of idea just made it crash.
hi Chris thank you for providing this videos its helping me so much but I have a question how can I convert it to an app by Android and iOS and if I want to let two forms the one where you write your name, press play how can I make it play with computer I have seen this video but I don't want to remove the second form. thank you for the knowledge you provided I hope you upload more about website such as streaming online videos and apps how its done again thank you I hope you answer my question
Thanks for this :) I have one more request. I am making a virtual OS (Basic logon screen, media player, browser etc)and Im stuck on how to call a class and run it from an icon. So when I click on my Tic-Tac-Toe icon (I have this entire program in a seperate class) I dont know how to open the program in a window?
I am trying to make the game tic-tac-toe (human against computer) and I do not know how to progress progressBar when it is the pc that plays. When it is me who plays and I click on one of the 9 buttons the progressebar goes all well. Can you help me
hi Chris. please help me how can i dissable buttons if theres a winner when im playing with ai because ai can make a move after i win. I try my best but still i cant figure out how to dissable buttons. please help me with this matter. thanks
hiii chris your videos are really rocking & made easy to understand but please show us how to put our app in windows store and earn money(app monetization) or publish our app into our website.!!!! your help would be appreciated
Hello, i know its a bit late but.. I have problem that application is working but when i start it and its against computer.. Its same as against player can somebody help me?
Hey Chris, I just wanted to modify the program a bit and wanted the computer to be 'X'. So for the first turn, if i use the "computer_make_move" method the AI always clicks the "A1" button as expected. So I decided to use the "look_for_open_space" method but this time the AI always clicks the "C3" button. So i was wondering as to why does this happen and is there a way by which we can select a button at random? Thanks.
Piyush Jaiswal, always clicking C3, is due to the order of the controls in the list [foreach (Control c in Controls)]. You could randomize this. I modified my look_for_open_space method and added a shuffleArray method. See pastebin for the code: pastebin.com/GpLSDc1N.
if the game ends in a draw, it throws an unhandled exception! Could someone please tell me how to over come this? the specific error is below. Thanks in advance (o_o) "An unhandled exception of type 'System.NullReferenceException' occurred in WindowsFormsApplication1.exe" is what the error says.
This is due to the code move.PerformClick(); because when a draw occurs there are no more spaces for the computer to click so therefore it doesn't have an "object" to click. To overcome this issue create an if statement containing the following >> if (turn_count != 9) move.PerformClick(); Using this if statement makes the computer only perform a click during the first 8 turns and when you have your final turn in order to create a draw (which is the 9th turn count) the computer will not perform another click due to the code "!=9:". This should hopefully fix your error, please let me know if you have any problems implementing the code!
When you play B2, C3, B1 and finally A3 the program encounters an error: System.NullReferenceException: "The object reference is not set on an instance of an object." so that does fix the draw but not the move it doesn't know to make when the game is practically a draw but fields are yet to be filled. The move counter will not solve this issue
Hi Chris, We are students from Turkey and we have project about tic-toc-toe also we made codes to part 3 but computer ai doesn't work.Part 4 codes not working. Can you send us the full codes thanks for answer
""Amazing video really helpful to all ""but i think there is a little problm in code i just add new method in b/w yours named "look_for_center" and under that B2 button is assigned b'coz there is only one center named B2.but when i play vs A.I and after pressing following buttons A1,C3,C2, i give the opportunity to AI to win , but he didnt make tic take toe , instead make a foolish move ,...kindly plz help
hey, i have a program using java where I have to make a tic tac toe game using the human user against the computer user could you please help me.. I'm so lost
You probably swapped round the conditions for X to win and O to win. He covered this in part 1. It's the inverse of what you think. If there is a winner and it's you're turn, the computer wins. When it's not you're turn, you win. It sounds confusing.
I found a guaranteed win method and I was wondering if you could help me with it. Basically when my first three moves are A3, C1 and C3 then i'll always win. For some reason the AI does not block my row. I checked your code and I can't seem to find the solution. Any chance you could check it out? P.S. I copied the code from your pastebin so I did not forget anything.
I should have watched the entire video before commenting this since I see you also noticed this problem at the end of the video. How would you go about this? Would this be fixed by making another if statement inside the other if statements where it basically checks if one sequal has been blocked the turn before?
M0481 the reason for this has more to do with tic tac toe logic, and then the "AI"s logic. in ANY tic tac toe game, if the first player is able to select three corners as their first three moves, that first player will always be the winner. Is becomes impossible to block all future winning moves
Very nice tutorial but your tic tac toe strategy is terrible. When the fist player opens, for example, with A1, then never answer with C3. His second move will be C1 or A3. You had to block that with B1 or A2. Than the player plays A3 or C1 and you had to block in two places. You lose.
thx Chris very helpful im ready for school now but I got an error in the computer_make_move method where I was getting an unhandled null reference exception but all is good I fix it with a try/catch :D here's the code for anyone who might have the same problem. try { Button move = null; //look for tic tac toe opportunities move = look_for_win_or_block("O"); //look for win if (move == null) { move = look_for_win_or_block("X"); //look for block if (move == null) { move = look_for_corner(); if (move == null) { move = look_for_open_space(); }//end if }//end if }//end if move.PerformClick(); } catch { }
If anyone is still around here and looking for a real challenging addition to this code: pastebin.com/McDBpDnK Add an additional priority for the computer to check middle button first, if empty, select it then iterate through the rest of the priorities
I started several times with this tutorial and halfway the project everything worked fine. I feft the project open for the night and Microsoft updated everything but all the method where put out of there context and it was one big file without + or - for the methods. I started again and scipt some things about de second form. Now a have a system that works fine but when there is a draw I get an message like this: Zie het einde van dit bericht voor meer informatie over het aanroepen van JIT-foutopsporing (Just In Time) in plaats van dit dialoogvenster. ************** Tekst van uitzondering ************** System.NullReferenceException: De objectverwijzing is niet op een exemplaar van een object ingesteld. bij TikTakTor.Form1.computer_make_move() in c:\Users\Han\Documents\Visual Studio 2012\Projects\TikTakTor\TikTakTor\Form1.cs:regel 83 bij TikTakTor.Form1.button_click(Object sender, EventArgs e) in c:\Users\Han\Documents\Visual Studio 2012\Projects\TikTakTor\TikTakTor\Form1.cs:regel 55 bij System.Windows.Forms.Control.OnClick(EventArgs e) bij System.Windows.Forms.Button.OnClick(EventArgs e) bij System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent) bij System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks) bij System.Windows.Forms.Control.WndProc(Message& m) bij System.Windows.Forms.ButtonBase.WndProc(Message& m) bij System.Windows.Forms.Button.WndProc(Message& m) bij System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m) bij System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m) bij System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam) ************** Geladen assembly's ************** mscorlib Assembly-versie: 4.0.0.0 Win32-versie: 4.0.30319.18444 built by: FX451RTMGDR CodeBase: file:///C:/Windows/Microsoft.NET/Framework/v4.0.30319/mscorlib.dll ---------------------------------------- TikTakTor Assembly-versie: 1.0.0.0 Win32-versie: 1.0.0.0 CodeBase: file:///C:/Users/Han/Documents/Visual%20Studio%202012/Projects/TikTakTor/TikTakTor/bin/Debug/TikTakTor.exe ---------------------------------------- System.Windows.Forms Assembly-versie: 4.0.0.0 Win32-versie: 4.0.30319.18408 built by: FX451RTMGREL CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Windows.Forms/v4.0_4.0.0.0__b77a5c561934e089/System.Windows.Forms.dll ---------------------------------------- System.Drawing Assembly-versie: 4.0.0.0 Win32-versie: 4.0.30319.18408 built by: FX451RTMGREL CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Drawing/v4.0_4.0.0.0__b03f5f7f11d50a3a/System.Drawing.dll ---------------------------------------- System Assembly-versie: 4.0.0.0 Win32-versie: 4.0.30319.18408 built by: FX451RTMGREL CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System/v4.0_4.0.0.0__b77a5c561934e089/System.dll ---------------------------------------- mscorlib.resources Assembly-versie: 4.0.0.0 Win32-versie: 4.0.30319.18408 built by: FX451RTMGREL CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/mscorlib.resources/v4.0_4.0.0.0_nl_b77a5c561934e089/mscorlib.resources.dll ---------------------------------------- System.Windows.Forms.resources Assembly-versie: 4.0.0.0 Win32-versie: 4.0.30319.18408 built by: FX451RTMGREL CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Windows.Forms.resources/v4.0_4.0.0.0_nl_b77a5c561934e089/System.Windows.Forms.resources.dll ---------------------------------------- System.Xml Assembly-versie: 4.0.0.0 Win32-versie: 4.0.30319.18408 built by: FX451RTMGREL CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Xml/v4.0_4.0.0.0__b77a5c561934e089/System.Xml.dll ---------------------------------------- ************** JIT-foutopsporing ************** Als u JIT-foutopsporing wilt inschakelen, moet in het configuratiebestand voor deze toepassing of computer (machine.config) de waarde jitDebugging in het gedeelte system.windows.forms zijn ingesteld. De toepassing moet ook zijn gecompileerd terwijl foutopsporing was ingeschakeld. Bijvoorbeeld: Wanneer JIT-foutopsporing is ingeschakeld, worden onverwerkte uitzonderingen naar het JIT-foutopsporingsprogramma gestuurd dat op de computer is geregistreerd en worden niet door dit dialoogvenster verwerkt.
I found the solution. I started the computer_makes move with: if (turn_count !=9) and the rest in brackets. You use everywhere if. I don't know why that works. I used if, else if etc. and thats work the same.
Hello, i'm a total noobie so please don't take my answer as 100% corect but as u can see that look_for_win_or_block returns a value that's exactly a name of one of the buttons from from1, let's say it return A3 so move (which was declared as a button) has the value A3 so move.PerformClick(); will click the button A3 from the example
Chris, as a fellow programmer, thanks for all the time and effort you put into this project. I learned a lot from your videos about Windows application development. Keep up the great work!
Great tutorial to follow as a complete beginner.
Had watched quite a few videos of C# for beginners, so thought i'd move onto a game.
This was greatly structured and easy to follow and learn along
oh man, this video is fantastic, it's 2020 and it helped me a lot, thank you very much and you have a new sub
Chris,
Your patience is beyond me. Not only do you show how to build this Tic Tac Toe program you take the time to cater to your commenters (i.e.3a and 4). I'm a really old guy (50+) and just decided to go get my BS in IT to keep my mind engaged. Your tutoring methods are so easy to follow and you explain stuff as well. Thanks for making these videos. I do have a question for you though. Once you create this game how do you get it out of Visual Studio and onto any PC? Like a packaged program I guess.
D Henson , Thanks for the kind words. With these really simple projects (like most of mine are), you can just go into the bin/debug folder of your project (after you have run it - i.e., the play button), and you'll see an executable (exe file). You can just double click to run the application. If you were looking to actually distribute what you build in a more professional way, you could add Install Shield's limited edition to Visual Studio, but it's a lot more involved. Not necessarily difficult, but takes some effort and a decent amount of trial and error to get it right. :)
Thanks Chris.
Can you make Tutorial TicTacToe with client server?
I need to add a socket using the TCP/IP protocol, how can I accomplish this?
Chris.
I think You should make another video in which Describe this please..either player 1's turn or player 2's turn
Why when the user wins, the computer also wins?
amazing videos, i kinda understood how windows forms works waaay better thanks to you.
Chris, i will say Thank you so much, because your tutorials help me to make my assignment :))
ill wait your new tutorial how to make another simple game with AI
is there a way to make it where the player gets to pick 2 player or vs computer
would u please provide us the complete project?
Great tutorials that help to get into Winforms, good job!
Hi,Chris, I have some questions. Your videos about Tic Tac Toe really helps me, but I got an error in the computer_make_move method where I was getting an unhandled null reference exception, method disableButtons doesn't working. When i finish the game, empty buttons stay active and I can click on them. How can I solve this problem? Please,help me(
I know this comment is 4 years old, but maybe someone else is wondering about the disableButtons method not working. You need to move the try catch in the disableButtons method inside the foreach, just like Chris did with the "New Game" menu item click event in part 2.
@@scy216 there’s another way to do it. Again I know this comment is 2 years old, but I was making a program and I got stuck on that same part. For about 4 hours, until I finally found another solution
foreach ( Control c in this.Controls )
{
TextBox tb = c as TextBox;
if ( null != tb )
{
}
}
I then switched text box to button and it worked.
sir please make another video in which make the game advance i mean use pictures and high graphics.
Great tutorial. But going for the corner square when you are player 2 is suicide, the only way player 1 doesn't win is if player 2 plays middle square on turn 2. You should only go for the corners if you are player 1. Assuming both players are playing the optimal way of course.
Thanks Chris for sharing this type of excellent work !
Would you be able to show how one might be able to display the game board or past games once called upon? for example, file, load game history, ( and in this new page you could click on game 1, game 2 etc.. and it would show the outcome of each game selected?
Great job Chris! Thanks for that example!
Hi thanks for your tutorial ...has a bug that the computer moves twice ? ...
I've got a little problem with the against_the_computer and There_Is_a_winner system. When player against the computer if player wins the computer make a move and is also declared a winner. Is there a way to lock out the computer when the winner is announce? Tried a couple of idea just made it crash.
hi there. this is my prblem too, anyway do you already know how to fix it? thanks.
move.PerformClick(); What id it?
hi Chris thank you for providing this videos its helping me so much but I have a question how can I convert it to an app by Android and iOS and if I want to let two forms the one where you write your name, press play how can I make it play with computer I have seen this video but I don't want to remove the second form. thank you for the knowledge you provided I hope you upload more about website such as streaming online videos and apps how its done again thank you I hope you answer my question
Thanks for this :) I have one more request. I am making a virtual OS (Basic logon screen, media player, browser etc)and Im stuck on how to call a class and run it from an icon. So when I click on my Tic-Tac-Toe icon (I have this entire program in a seperate class) I dont know how to open the program in a window?
Hi Chris,
Can you add writing win results in some database(postgresql or some other) and make some list of best players?
I am trying to make the game tic-tac-toe (human against computer) and I do not know how to progress progressBar when it is the pc that plays. When it is me who plays and I click on one of the 9 buttons the progressebar goes all well. Can you help me
hi Chris. please help me how can i dissable buttons if theres a winner when im playing with ai because ai can make a move after i win. I try my best but still i cant figure out how to dissable buttons. please help me with this matter. thanks
Thanks for the tutorial. What do I do if I want to make a button, that says vs Computer?
I used it in my own Tic Tac Toe but in vb.net.
Thank you
hiii chris your videos are really rocking & made easy to understand but please show us how to put our app in windows store and earn money(app monetization) or publish our app into our website.!!!! your help would be appreciated
You can help me how to create an AI engine for the tic tac toe game with Hill-Climbing search???
thanks chris
Sir, can I know which Algorithm you are using? Negamax or Minimax Algorithm?
chris can you make Ai move in minimax algorithm to be more understand
You did awesome job! Thank you and keep on making tutorial videos
Hello, i know its a bit late but.. I have problem that application is working but when i start it and its against computer.. Its same as against player can somebody help me?
The app stopt working after adding all this cant even press any button to fill a box anymore
Hey Chris,
I just wanted to modify the program a bit and wanted the computer to be 'X'. So for the first turn, if i use the "computer_make_move" method the AI always clicks the "A1" button as expected. So I decided to use the "look_for_open_space" method but this time the AI always clicks the "C3" button. So i was wondering as to why does this happen and is there a way by which we can select a button at random?
Thanks.
Piyush Jaiswal, always clicking C3, is due to the order of the controls in the list [foreach (Control c in Controls)]. You could randomize this. I modified my look_for_open_space method and added a shuffleArray method. See pastebin for the code: pastebin.com/GpLSDc1N.
Chris Merritt, thanks a ton again for all your help and videos.
how to put an exception by asking the player if he wants to play again?? can someone teach me how to do this??
if the game ends in a draw, it throws an unhandled exception! Could someone please tell me how to over come this? the specific error is below. Thanks in advance (o_o)
"An unhandled exception of type 'System.NullReferenceException' occurred in WindowsFormsApplication1.exe" is what the error says.
This is due to the code move.PerformClick(); because when a draw occurs there are no more spaces for the computer to click so therefore it doesn't have an "object" to click. To overcome this issue create an if statement containing the following >> if (turn_count != 9)
move.PerformClick();
Using this if statement makes the computer only perform a click during the first 8 turns and when you have your final turn in order to create a draw (which is the 9th turn count) the computer will not perform another click due to the code "!=9:". This should hopefully fix your error, please let me know if you have any problems implementing the code!
Thank you soooo much, i spent 2 hours to finde the error
End of button_click event could looks like:
if ((!turn) && (against_computer) && (turn_count < 9))
computer_make_move();
When you play B2, C3, B1 and finally A3 the program encounters an error:
System.NullReferenceException: "The object reference is not set on an instance of an object." so that does fix the draw but not the move it doesn't know to make when the game is practically a draw but fields are yet to be filled. The move counter will not solve this issue
@@BnBProductionzzz thankyou so much, cause your information can help each other :))
chriss can u make computer vs human tictac toe game
Hi Chris,
We are students from Turkey and we have project about tic-toc-toe also we made codes to part 3 but computer ai doesn't work.Part 4 codes not working. Can you send us the full codes thanks for answer
""Amazing video really helpful to all ""but i think there is a little problm in code
i just add new method in b/w yours named "look_for_center" and under that B2 button is assigned b'coz there is only one center named B2.but when i play vs A.I
and after pressing following buttons A1,C3,C2, i give the opportunity to AI to win , but he didnt make tic take toe , instead make a foolish move ,...kindly plz help
hey, i have a program using java where I have to make a tic tac toe game using the human user against the computer user could you please help me.. I'm so lost
Hi there ive got a problem, when i win against the computer it awards the computer a point too how do i fix this
You probably swapped round the conditions for X to win and O to win. He covered this in part 1. It's the inverse of what you think. If there is a winner and it's you're turn, the computer wins. When it's not you're turn, you win. It sounds confusing.
your code are use minimax algorithm?
Ohh i get stuck in some bad error. When all buttons are click i got error Unhadled ....something like that :S
How do look_for_open_space methods of work?
great tutorial!
hello sir, what is the algorithm you use for this AI ???
Thanks Chris very much!
is it minmax algorithm ?
alguna persona que pueda pasar el programa xd
Can you do this on visual basic.net?
I found a guaranteed win method and I was wondering if you could help me with it.
Basically when my first three moves are A3, C1 and C3 then i'll always win.
For some reason the AI does not block my row.
I checked your code and I can't seem to find the solution.
Any chance you could check it out?
P.S. I copied the code from your pastebin so I did not forget anything.
I should have watched the entire video before commenting this since I see you also noticed this problem at the end of the video.
How would you go about this? Would this be fixed by making another if statement inside the other if statements where it basically checks if one sequal has been blocked the turn before?
M0481 the reason for this has more to do with tic tac toe logic, and then the "AI"s logic. in ANY tic tac toe game, if the first player is able to select three corners as their first three moves, that first player will always be the winner. Is becomes impossible to block all future winning moves
please i want the source code
can u send source code please
hi, I the AI doesn't do anything but i get no errors? could someone please help? thanks.
namespace TicTacToe
{
public partial class TicTacToe : Form
{
bool turn = true;// true = X turn; false = Y turn
bool against_computer = false;
int turn_count = 0;
//static String player1, player2;
public TicTacToe()
{
InitializeComponent();
}
/*
public static void SetPlayerNames(String n1, String n2)
{
player1 = n1;
player2 = n2;
}
*/
private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
{
MessageBox.Show(" Created by Jack Roper, 6/10/2016.", "Tic Tac Toe about..");
}
private void exitToolStripMenuItem_Click(object sender, EventArgs e)
{
Application.Exit();
}
private void button_click(object sender, EventArgs e)
{
Button b = (Button)sender;
if (turn)
b.Text = "X";
else
b.Text = "O";
turn = !turn;
b.Enabled = false;
turn_count++;
checkForWinner();
if ((!turn) && (against_computer))
{
computer_make_move();
}
}
private void computer_make_move()
{
//priority 1: get tick tac toe
//priority 2: block x tic tac toe
//priority 3: go for corner space
//priority 4: pick open space
Button move = null;
//look for tic tac toe opportunities
move = look_for_win_or_block("O"); //look for win
if (move == null)
{
move = look_for_win_or_block("X"); //look for block
if (move == null)
{
move = look_for_corner();
if (move == null)
{
move = look_for_open_space();
}//end if
}//end if
}//end if
move.PerformClick();
}
private Button look_for_open_space()
{
Console.WriteLine("Looking for open space");
Button b = null;
foreach (Control c in Controls)
{
b = c as Button;
if (b != null)
{
if (b.Text == "")
return b;
}//end if
}//end if
return null;
}
private Button look_for_corner()
{
Console.WriteLine("Looking for corner");
if (A1.Text == "O")
{
if (A3.Text == "")
return A3;
if (C3.Text == "")
return C3;
if (C1.Text == "")
return C1;
}
if (A3.Text == "O")
{
if (A1.Text == "")
return A1;
if (C3.Text == "")
return C3;
if (C1.Text == "")
return C1;
}
if (C3.Text == "O")
{
if (A1.Text == "")
return A3;
if (A3.Text == "")
return A3;
if (C1.Text == "")
return C1;
}
if (C1.Text == "O")
{
if (A1.Text == "")
return A3;
if (A3.Text == "")
return A3;
if (C3.Text == "")
return C3;
}
if (A1.Text == "")
return A1;
if (A3.Text == "")
return A3;
if (C1.Text == "")
return C1;
if (C3.Text == "")
return C3;
return null;
}
private Button look_for_win_or_block(string mark)
{
Console.WriteLine("Looking for win or block: " + mark);
//HORIZONTAL TESTS
if ((A1.Text == mark) && (A2.Text == mark) && (A3.Text == ""))
return A3;
if ((A2.Text == mark) && (A3.Text == mark) && (A1.Text == ""))
return A1;
if ((A1.Text == mark) && (A3.Text == mark) && (A2.Text == ""))
return A2;
if ((B1.Text == mark) && (B2.Text == mark) && (B3.Text == ""))
return B3;
if ((B2.Text == mark) && (B3.Text == mark) && (B1.Text == ""))
return B1;
if ((B1.Text == mark) && (B3.Text == mark) && (B2.Text == ""))
return B2;
if ((C1.Text == mark) && (C2.Text == mark) && (C3.Text == ""))
return C3;
if ((C2.Text == mark) && (C3.Text == mark) && (C1.Text == ""))
return C1;
if ((C1.Text == mark) && (C3.Text == mark) && (C2.Text == ""))
return C2;
//VERTICAL TESTS
if ((A1.Text == mark) && (B1.Text == mark) && (C1.Text == ""))
return C1;
if ((B1.Text == mark) && (C1.Text == mark) && (A1.Text == ""))
return A1;
if ((A1.Text == mark) && (C1.Text == mark) && (B1.Text == ""))
return B1;
if ((A2.Text == mark) && (B2.Text == mark) && (C2.Text == ""))
return C2;
if ((B2.Text == mark) && (C2.Text == mark) && (A2.Text == ""))
return A2;
if ((A2.Text == mark) && (C2.Text == mark) && (B2.Text == ""))
return B2;
if ((A3.Text == mark) && (B3.Text == mark) && (C3.Text == ""))
return C3;
if ((B3.Text == mark) && (C3.Text == mark) && (A3.Text == ""))
return A3;
if ((A3.Text == mark) && (C3.Text == mark) && (B3.Text == ""))
return B3;
//DIAGONAL TESTS
if ((A1.Text == mark) && (B2.Text == mark) && (C3.Text == ""))
return C3;
if ((B2.Text == mark) && (C3.Text == mark) && (A1.Text == ""))
return A1;
if ((A1.Text == mark) && (C3.Text == mark) && (B2.Text == ""))
return B2;
if ((A3.Text == mark) && (B2.Text == mark) && (C1.Text == ""))
return C1;
if ((B2.Text == mark) && (C1.Text == mark) && (A3.Text == ""))
return A3;
if ((A3.Text == mark) && (C1.Text == mark) && (B2.Text == ""))
return B2;
return null;
}
private void checkForWinner()
{
bool there_is_a_winner = false;
//horizontal checks
if ((A1.Text == A2.Text) && (A2.Text == A3.Text) && (!A1.Enabled))
there_is_a_winner = true;
else if ((B1.Text == B2.Text) && (B2.Text == B3.Text) && (!B1.Enabled))
there_is_a_winner = true;
else if ((C1.Text == C2.Text) && (C2.Text == C3.Text) && (!C1.Enabled))
there_is_a_winner = true;
//vertical checks
else if ((A1.Text == B1.Text) && (B1.Text == C1.Text) && (!A1.Enabled))
there_is_a_winner = true;
else if ((A2.Text == B2.Text) && (B2.Text == C2.Text) && (!A2.Enabled))
there_is_a_winner = true;
else if ((A3.Text == B3.Text) && (B3.Text == C3.Text) && (!A3.Enabled))
there_is_a_winner = true;
//diagonal checks
else if ((A1.Text == B2.Text) && (B2.Text == C3.Text) && (!A1.Enabled))
there_is_a_winner = true;
else if ((A3.Text == B2.Text) && (B2.Text == C1.Text) && (!C1.Enabled))
there_is_a_winner = true;
someone please give the source code
Wow thank you ❤
Very nice tutorial but your tic tac toe strategy is terrible. When the fist player opens, for example, with A1, then never answer with C3. His second move will be C1 or A3. You had to block that with B1 or A2. Than the player plays A3 or C1 and you had to block in two places. You lose.
source code please
Thanks!!!!
Hey Chris , I dont get why The AI crashes when its a draw vs. Computer. Does it happen to yu?
Where is the mistake in this code?
private void computer_move()
{
Button move = null;
move = look_for_win_or_block("O");
if (move == null)
{
move = look_for_win_or_block("X");
if (move == null)
{
move = look_for_corner();
if (move == null)
{
move = look_for_open_space("O");
}
}
}
move.PerformClick();
}
move=look_for_open_space();
thx Chris very helpful im ready for school now but I got an error in the computer_make_move method where I was getting an unhandled null reference exception but all is good I fix it with a try/catch :D here's the code for anyone who might have the same problem.
try
{
Button move = null;
//look for tic tac toe opportunities
move = look_for_win_or_block("O"); //look for win
if (move == null)
{
move = look_for_win_or_block("X"); //look for block
if (move == null)
{
move = look_for_corner();
if (move == null)
{
move = look_for_open_space();
}//end if
}//end if
}//end if
move.PerformClick();
}
catch { }
if (turn_count != 9)
move.PerformClick();
shorter code~
If anyone is still around here and looking for a real challenging addition to this code:
pastebin.com/McDBpDnK
Add an additional priority for the computer to check middle button first, if empty, select it
then iterate through the rest of the priorities
I can beat urs every time with top left, then bottom right, then bottom left, then either other one
I started several times with this tutorial and halfway the project everything worked fine. I feft the project open for the night and Microsoft updated everything but all the method where put out of there context and it was one big file without + or - for the methods. I started again and scipt some things about de second form. Now a have a system that works fine but when there is a draw I get an message like this:
Zie het einde van dit bericht voor meer informatie over het aanroepen
van JIT-foutopsporing (Just In Time) in plaats van dit dialoogvenster.
************** Tekst van uitzondering **************
System.NullReferenceException: De objectverwijzing is niet op een exemplaar van een object ingesteld.
bij TikTakTor.Form1.computer_make_move() in c:\Users\Han\Documents\Visual Studio 2012\Projects\TikTakTor\TikTakTor\Form1.cs:regel 83
bij TikTakTor.Form1.button_click(Object sender, EventArgs e) in c:\Users\Han\Documents\Visual Studio 2012\Projects\TikTakTor\TikTakTor\Form1.cs:regel 55
bij System.Windows.Forms.Control.OnClick(EventArgs e)
bij System.Windows.Forms.Button.OnClick(EventArgs e)
bij System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
bij System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
bij System.Windows.Forms.Control.WndProc(Message& m)
bij System.Windows.Forms.ButtonBase.WndProc(Message& m)
bij System.Windows.Forms.Button.WndProc(Message& m)
bij System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
bij System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
bij System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
************** Geladen assembly's **************
mscorlib
Assembly-versie: 4.0.0.0
Win32-versie: 4.0.30319.18444 built by: FX451RTMGDR
CodeBase: file:///C:/Windows/Microsoft.NET/Framework/v4.0.30319/mscorlib.dll
----------------------------------------
TikTakTor
Assembly-versie: 1.0.0.0
Win32-versie: 1.0.0.0
CodeBase: file:///C:/Users/Han/Documents/Visual%20Studio%202012/Projects/TikTakTor/TikTakTor/bin/Debug/TikTakTor.exe
----------------------------------------
System.Windows.Forms
Assembly-versie: 4.0.0.0
Win32-versie: 4.0.30319.18408 built by: FX451RTMGREL
CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Windows.Forms/v4.0_4.0.0.0__b77a5c561934e089/System.Windows.Forms.dll
----------------------------------------
System.Drawing
Assembly-versie: 4.0.0.0
Win32-versie: 4.0.30319.18408 built by: FX451RTMGREL
CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Drawing/v4.0_4.0.0.0__b03f5f7f11d50a3a/System.Drawing.dll
----------------------------------------
System
Assembly-versie: 4.0.0.0
Win32-versie: 4.0.30319.18408 built by: FX451RTMGREL
CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System/v4.0_4.0.0.0__b77a5c561934e089/System.dll
----------------------------------------
mscorlib.resources
Assembly-versie: 4.0.0.0
Win32-versie: 4.0.30319.18408 built by: FX451RTMGREL
CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/mscorlib.resources/v4.0_4.0.0.0_nl_b77a5c561934e089/mscorlib.resources.dll
----------------------------------------
System.Windows.Forms.resources
Assembly-versie: 4.0.0.0
Win32-versie: 4.0.30319.18408 built by: FX451RTMGREL
CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Windows.Forms.resources/v4.0_4.0.0.0_nl_b77a5c561934e089/System.Windows.Forms.resources.dll
----------------------------------------
System.Xml
Assembly-versie: 4.0.0.0
Win32-versie: 4.0.30319.18408 built by: FX451RTMGREL
CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Xml/v4.0_4.0.0.0__b77a5c561934e089/System.Xml.dll
----------------------------------------
************** JIT-foutopsporing **************
Als u JIT-foutopsporing wilt inschakelen, moet in het configuratiebestand voor deze
toepassing of computer (machine.config) de waarde
jitDebugging in het gedeelte system.windows.forms zijn ingesteld.
De toepassing moet ook zijn gecompileerd terwijl foutopsporing
was ingeschakeld.
Bijvoorbeeld:
Wanneer JIT-foutopsporing is ingeschakeld, worden onverwerkte uitzonderingen
naar het JIT-foutopsporingsprogramma gestuurd dat op de computer is geregistreerd
en worden niet door dit dialoogvenster verwerkt.
I found the solution. I started the computer_makes move with: if (turn_count !=9) and the rest in brackets.
You use everywhere if. I don't know why that works. I used if, else if etc. and thats work the same.
move.PerformClick(); What id it?
Hello, i'm a total noobie so please don't take my answer as 100% corect but as u can see that look_for_win_or_block returns a value that's exactly a name of one of the buttons from from1, let's say it return A3 so move (which was declared as a button) has the value A3 so move.PerformClick(); will click the button A3 from the example
@@valeanalex1918 thank you very much for your help
@@valeanalex1918 move.PerformClick() is this a function that declared ? can u give me the code for this performclick