private void Computer_Move_Click(object sender, EventArgs e)
{
Graphics g = this.CreateGraphics();
Random random = new Random();
//int pX = 15;
//int pY = 105;
int pX = random.Next(0, 200);
int pY = random.Next(0, 200);
Rectangle square_one = new Rectangle(10,20,30,40);
Rectangle square_two = new Rectangle(80, 90, 30, 40);
Rectangle square_three = new Rectangle(160, 170, 30, 40);
Rectangle square_four = new Rectangle(10, 20, 100, 110);
Rectangle square_five = new Rectangle(80, 90, 100, 110);
Rectangle square_six = new Rectangle(160, 170, 100, 110);
Rectangle square_seven = new Rectangle(10, 20, 160, 170);
Rectangle square_eight = new Rectangle(80, 90, 160, 170);
Rectangle square_nine = new Rectangle(160, 170, 160, 170);
while (pX == 15 || pX == 85 || pX == 165 || pY == 35 || pY == 105 || pY == 165)
{
if (pX >= square_one.X && pX = square_one.Width && pY = square_two.X && pX = square_two.Width && pY = square_three.X && pX = square_three.Width && pY = square_four.X && pX = square_four.Width && pY = square_five.X && pX = square_five.Width && pY = square_six.X && pX = square_six.Width && pY = square_seven.X && pX = square_seven.Width && pY = square_eight.X && pX = square_eight.Width && pY = square_nine.X && pX = square_nine.Width && pY
tic tac toe ai
Started by phil67rpg, Feb 22 2012 07:12 PM
6 replies to this topic
#1 Members - Reputation: 94
Posted 22 February 2012 - 07:12 PM
well I am making a tic tac toe game using c# and gdi+.I want to implement a very simple AI using random numbers to draw "O"'s on the screen.here is the code I am working on.
Ad:
#2 Members - Reputation: 1217
Posted 22 February 2012 - 07:27 PM
phil67rpg, on 22 February 2012 - 07:12 PM, said:
well I am making a tic tac toe game using c# and gdi+.I want to implement a very simple AI using random numbers to draw "O"'s on the screen.here is the code I am working on.
FYI, when you want to copy 'n' paste code, first paste it into notepad, then copy it from notepad into GameDev's post editor. That way it'll remove the formatting and GameDev won't mangle your code like it is right now. For the rest of the world, here's what I could salvage from the code:
private void Computer_Move_Click(object sender, EventArgs e)
{
Graphics g = this.CreateGraphics();
Random random = new Random();
//int pX = 15;
//int pY = 105;
int pX = random.Next(0, 200);
int pY = random.Next(0, 200);
Rectangle square_one = new Rectangle(10,20,30,40);
Rectangle square_two = new Rectangle(80, 90, 30, 40);
Rectangle square_three = new Rectangle(160, 170, 30, 40);
Rectangle square_four = new Rectangle(10, 20, 100, 110);
Rectangle square_five = new Rectangle(80, 90, 100, 110);
Rectangle square_six = new Rectangle(160, 170, 100, 110);
Rectangle square_seven = new Rectangle(10, 20, 160, 170);
Rectangle square_eight = new Rectangle(80, 90, 160, 170);
Rectangle square_nine = new Rectangle(160, 170, 160, 170);
while (pX == 15 || pX == 85 || pX == 165 || pY == 35 || pY == 105 || pY == 165)
{
if (pX >= square_one.X && pX <= square_one.Y && pY >= square_one.Width && pY <= square_one.Height)
{
g.DrawString("O", new Font("Verdana", 20), new SolidBrush(Color.Green), new Point(pX, pY));
}
if (pX >= square_two.X && pX <= square_two.Y && pY >= square_two.Width && pY <= square_two.Height)
{
g.DrawString("O", new Font("Verdana", 20), new SolidBrush(Color.Green), new Point(pX, pY));
}
if (pX >= square_three.X && pX <= square_three.Y && pY >= square_three.Width && pY <= square_three.Height)
{
g.DrawString("O", new Font("Verdana", 20), new SolidBrush(Color.Green), new Point(pX, pY));
}
if (pX >= square_four.X && pX <= square_four.Y && pY >= square_four.Width && pY <= square_four.Height)
{
g.DrawString("O", new Font("Verdana", 20), new SolidBrush(Color.Green), new Point(pX, pY));
}
if (pX >= square_five.X && pX <= square_five.Y && pY >= square_five.Width && pY <= square_five.Height)
{
g.DrawString("O", new Font("Verdana", 20), new SolidBrush(Color.Green), new Point(pX, pY));
}
if (pX >= square_six.X && pX <= square_six.Y && pY >= square_six.Width && pY <= square_six.Height)
{
g.DrawString("O", new Font("Verdana", 20), new SolidBrush(Color.Green), new Point(pX, pY));
}
if (pX >= square_seven.X && pX <= square_seven.Y && pY >= square_seven.Width && pY <= square_seven.Height)
{
g.DrawString("O", new Font("Verdana", 20), new SolidBrush(Color.Green), new Point(pX, pY));
}
if (pX >= square_eight.X && pX <= square_eight.Y && pY >= square_eight.Width && pY <= square_eight.Height)
{
g.DrawString("O", new Font("Verdana", 20), new SolidBrush(Color.Green), new Point(pX, pY));
}
if (pX >= square_nine.X && pX <= square_nine.Y && pY >= square_nine.Width && pY <= square_nine.Height)
{
g.DrawString("O", new Font("Verdana", 20), new SolidBrush(Color.Green), new Point(pX, pY));
}
break;
}
g.Dispose();
}
}
}
Just a suggestion on simplifying your code: make an array of rectangles, and then iterate through each rectangle and perform the test. Something like this (note that this code is untested, but hopefully it gives you an idea of how to simplify things):
private void Computer_Move_Click(object sender, EventArgs e)
{
Graphics g = this.CreateGraphics();
Random random = new Random();
//int pX = 15;
//int pY = 105;
int pX = random.Next(0, 200);
int pY = random.Next(0, 200);
Rectangle[] squares = new Rectangle[9];
squares[0] = new Rectangle(10,20,30,40);
squares[1] = new Rectangle(80, 90, 30, 40);
squares[2] = new Rectangle(160, 170, 30, 40);
squares[3] = new Rectangle(10, 20, 100, 110);
squares[4] = new Rectangle(80, 90, 100, 110);
squares[5] = new Rectangle(160, 170, 100, 110);
squares[6] = new Rectangle(10, 20, 160, 170);
squares[7] = new Rectangle(80, 90, 160, 170);
squares[8] = new Rectangle(160, 170, 160, 170);
while (pX == 15 || pX == 85 || pX == 165 || pY == 35 || pY == 105 || pY == 165)
{
for (int i = 0; i < squares.Length; ++i)
{
if (pX >= squares[i].X && pX <= squares[i].Y && pY >= squares[i].Width && pY <= squares[i].Height)
{
g.DrawString("O", new Font("Verdana", 20), new SolidBrush(Color.Green), new Point(pX, pY));
}
}
break; // Also, why do you have this break here???
}
g.Dispose();
}
}
}
[ Realistic Rendering ] [ School + Dublin = Boom ] [ I've been ninja'd 70 times ] [ f.k.a. MikeTacular ] [ My Blog ]
#3 Members - Reputation: 94
Posted 22 February 2012 - 07:36 PM
thanks corn,sorry about the mangled code.well my question is how do I go about tackling an AI problem.all I want to do is to get the computer to draw an "O" in the appropriate every time I click a button on the form.this is the first game in which I have tried to involve some AI to.go easy on me its my first time.I am learning alot about c# and gdi+ by doing this project.
#4 Members - Reputation: 1217
Posted 22 February 2012 - 07:43 PM
phil67rpg, on 22 February 2012 - 07:36 PM, said:
thanks corn,sorry about the mangled code.well my question is how do I go about tackling an AI problem.all I want to do is to get the computer to draw an "O" in the appropriate every time I click a button on the form.this is the first game in which I have tried to involve some AI to.go easy on me its my first time.I am learning alot about c# and gdi+ by doing this project.
Personal nitpick that makes reading your posts easier: put a space after ',' and '.' and capitalize the first letter after '.' like in my posts. I also made a quick edit to my post about simplifying your code. You may have seen it, but I think I made the changes just after your second post.
[ Realistic Rendering ] [ School + Dublin = Boom ] [ I've been ninja'd 70 times ] [ f.k.a. MikeTacular ] [ My Blog ]
#6 Members - Reputation: 112
Posted 01 March 2012 - 10:53 AM
Back in College here's how I solved the problem:
T3 has 3 rows, 3 columns and 2 diagonals. I called these 8 things a Set.
Assume I'm playing X. Negotiate the sets in this order
If any Set has X X - (In any order) then play my X in the blank to win
If any Set has O O - (in any order) then play an X in the blank to block
If any set contains X - - (in any order) then see if either of the blanks intersects with another set with the value X - - and play there. Otherwhise, play on a random blank.
If any set contains O - - (in any order) then see if either of the blanks intersects with another set with the value O - - and play there. Otherwhise, play on a random blank.
Find a set with - - - and play any blank.
If the only move left is in a set with X O - then the game is tied.
This simple AI will play an almost perfect game. To make it perfect you simply add some code to pick the best first move. Once that's done this system will find the best next move.
T3 has 3 rows, 3 columns and 2 diagonals. I called these 8 things a Set.
Assume I'm playing X. Negotiate the sets in this order
If any Set has X X - (In any order) then play my X in the blank to win
If any Set has O O - (in any order) then play an X in the blank to block
If any set contains X - - (in any order) then see if either of the blanks intersects with another set with the value X - - and play there. Otherwhise, play on a random blank.
If any set contains O - - (in any order) then see if either of the blanks intersects with another set with the value O - - and play there. Otherwhise, play on a random blank.
Find a set with - - - and play any blank.
If the only move left is in a set with X O - then the game is tied.
This simple AI will play an almost perfect game. To make it perfect you simply add some code to pick the best first move. Once that's done this system will find the best next move.
#7 Members - Reputation: 1698
Posted 01 March 2012 - 12:47 PM
It's more instructive to write an AI that figures out what to play by itself. You can use on of these:
* Minimax
* Monte Carlo Tree Search
* Retrograde analysis
* Minimax
* Monte Carlo Tree Search
* Retrograde analysis


















