I am working on the tic tac toe problem and i need help with the AI part i got it to work for 1v1 but it needs to be 1vAI

I was thinking that easiest was to to program something like "when player clicks this squaure, AI will click anything"

Should I create another temp button then everytime
tempbutton is used make tempbutton2 also move?

Belowe is the code for the clickhandler part

Thanks

private void ClickHandler(object sender, System.EventArgs e)

{

Button tempButton = (Button)sender;

if( this._isGameOver )

{

MessageBox.Show("please start new game!","Game End",MessageBoxButtons.OK);

return; 

}

if( tempButton.Text != "" ) 

{

return;

}

if( _isX ) 

tempButton.Text = "X";

else

tempButton.Text = "O";

_isX = !_isX; 

this._isGameOver = result.CheckWinner(_buttonArray );

}

}

Here's the basic structure you want to aim for:
After the person has taken their go, rotate the go for the next user (like multi-player).
Now write an AI function or class that returns the position in the array where the AI would like to go.
Set the position in the array that the AI() method returns equal to the computers 'go' (O or X).
Perhaps a boolean variable that denotes if your in single play or multiplayer and an if statement for when the button is clicked?

After this if statement which contains the extra code to run for a multi-player game, just rotate the user back to the human players go and let it continue with the code it would run for single player.

That should get you going, if you run into issues post back :)

Edited 6 Years Ago by Medalgod: n/a

Ok i added a computer class (at the bottom)

I am not sure how now implment it. I think it the comptuer needs to move everytime after the player moves.

I am really confused because i used buttons to create the tic tac toe problems and the the tuturiol I looked it used an array.

would the comptuer class be implemented in the ClickHandler class?

Thanks

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;


namespace tictactoe
{
	/// <summary>
	/// Summary description for Form1.
	/// </summary>
	public class Form1 : System.Windows.Forms.Form
	{
		private System.Windows.Forms.Button but9;
		private System.Windows.Forms.Button but8;
		private System.Windows.Forms.Button but7;
		private System.Windows.Forms.Button but6;
		private System.Windows.Forms.Button but5;
		private System.Windows.Forms.Button but4;
		private System.Windows.Forms.Button but3;
		private System.Windows.Forms.Button but2;
		private System.Windows.Forms.Button but1;
		private System.Windows.Forms.MainMenu mainMenu1;
		private System.Windows.Forms.MenuItem menuItem1;
		private System.Windows.Forms.MenuItem menuItem2;

        
		private System.Windows.Forms.Button [] _buttonArray; 
		private bool _isX;
        private bool _isGameOver;
        private MenuItem menuItem3;
        private IContainer components;

		public Form1()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();

			//
			// TODO: Add any constructor code after InitializeComponent call
			//
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if (components != null) 
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
            this.components = new System.ComponentModel.Container();
            this.but9 = new System.Windows.Forms.Button();
            this.but8 = new System.Windows.Forms.Button();
            this.but7 = new System.Windows.Forms.Button();
            this.but6 = new System.Windows.Forms.Button();
            this.but5 = new System.Windows.Forms.Button();
            this.but4 = new System.Windows.Forms.Button();
            this.but3 = new System.Windows.Forms.Button();
            this.but2 = new System.Windows.Forms.Button();
            this.but1 = new System.Windows.Forms.Button();
            this.mainMenu1 = new System.Windows.Forms.MainMenu(this.components);
            this.menuItem1 = new System.Windows.Forms.MenuItem();
            this.menuItem2 = new System.Windows.Forms.MenuItem();
            this.menuItem3 = new System.Windows.Forms.MenuItem();
            this.SuspendLayout();
            // 
            // but9
            // 
            this.but9.Font = new System.Drawing.Font("Microsoft Sans Serif", 24F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.but9.Location = new System.Drawing.Point(104, 104);
            this.but9.Name = "but9";
            this.but9.Size = new System.Drawing.Size(50, 50);
            this.but9.TabIndex = 18;
            // 
            // but8
            // 
            this.but8.Font = new System.Drawing.Font("Microsoft Sans Serif", 24F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.but8.Location = new System.Drawing.Point(56, 104);
            this.but8.Name = "but8";
            this.but8.Size = new System.Drawing.Size(50, 50);
            this.but8.TabIndex = 17;
            // 
            // but7
            // 
            this.but7.Font = new System.Drawing.Font("Microsoft Sans Serif", 24F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.but7.Location = new System.Drawing.Point(8, 104);
            this.but7.Name = "but7";
            this.but7.Size = new System.Drawing.Size(50, 50);
            this.but7.TabIndex = 16;
            // 
            // but6
            // 
            this.but6.Font = new System.Drawing.Font("Microsoft Sans Serif", 24F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.but6.Location = new System.Drawing.Point(104, 56);
            this.but6.Name = "but6";
            this.but6.Size = new System.Drawing.Size(50, 50);
            this.but6.TabIndex = 15;
            // 
            // but5
            // 
            this.but5.Font = new System.Drawing.Font("Microsoft Sans Serif", 24F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.but5.Location = new System.Drawing.Point(56, 56);
            this.but5.Name = "but5";
            this.but5.Size = new System.Drawing.Size(50, 50);
            this.but5.TabIndex = 14;
            // 
            // but4
            // 
            this.but4.Font = new System.Drawing.Font("Microsoft Sans Serif", 24F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.but4.Location = new System.Drawing.Point(8, 56);
            this.but4.Name = "but4";
            this.but4.Size = new System.Drawing.Size(50, 50);
            this.but4.TabIndex = 13;
            // 
            // but3
            // 
            this.but3.Font = new System.Drawing.Font("Microsoft Sans Serif", 24F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.but3.Location = new System.Drawing.Point(104, 8);
            this.but3.Name = "but3";
            this.but3.Size = new System.Drawing.Size(50, 50);
            this.but3.TabIndex = 12;
            // 
            // but2
            // 
            this.but2.Font = new System.Drawing.Font("Microsoft Sans Serif", 24F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.but2.Location = new System.Drawing.Point(56, 8);
            this.but2.Name = "but2";
            this.but2.Size = new System.Drawing.Size(50, 50);
            this.but2.TabIndex = 11;
            // 
            // but1
            // 
            this.but1.Font = new System.Drawing.Font("Microsoft Sans Serif", 24F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.but1.Location = new System.Drawing.Point(8, 8);
            this.but1.Name = "but1";
            this.but1.Size = new System.Drawing.Size(50, 50);
            this.but1.TabIndex = 10;
            // 
            // mainMenu1
            // 
            this.mainMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
            this.menuItem1});
            // 
            // menuItem1
            // 
            this.menuItem1.Index = 0;
            this.menuItem1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
            this.menuItem2,
            this.menuItem3});
            this.menuItem1.Text = "File";
            // 
            // menuItem2
            // 
            this.menuItem2.Index = 0;
            this.menuItem2.Text = "New";
            this.menuItem2.Click += new System.EventHandler(this.menuItem2_Click);
            // 
            // menuItem3
            // 
            this.menuItem3.Index = 1;
            this.menuItem3.Text = "Answer";
           
            // 
            // Form1
            // 
            this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
            this.ClientSize = new System.Drawing.Size(160, 161);
            this.Controls.Add(this.but9);
            this.Controls.Add(this.but8);
            this.Controls.Add(this.but7);
            this.Controls.Add(this.but6);
            this.Controls.Add(this.but5);
            this.Controls.Add(this.but4);
            this.Controls.Add(this.but3);
            this.Controls.Add(this.but2);
            this.Controls.Add(this.but1);
            this.Menu = this.mainMenu1;
            this.Name = "Form1";
            this.Text = "Form1";
            this.Load += new System.EventHandler(this.Form1_Load);
            this.ResumeLayout(false);

		}
		#endregion

		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		[STAThread]
		static void Main() 
		{
			Application.Run(new Form1());
		}

		private void Form1_Load(object sender, System.EventArgs e)
		{
			_buttonArray = new Button[9] {but1, but2, but3, but4, but5, but6, but7, but8, but9};
			for(int i = 0; i < 9; i++)
				this._buttonArray[i].Click += new System.EventHandler(this.ClickHandler);
			InitTicTacToe(); 
		}

		private void menuItem2_Click(object sender, System.EventArgs e)
		{
			InitTicTacToe();
            
		}

		public void InitTicTacToe()
		{
			for(int i=0;i<9;i++)
			{
				_buttonArray[i].Text = "";
				_buttonArray[i].ForeColor = Color.Black;
				_buttonArray[i].BackColor = Color.LightGray;
				_buttonArray[i].Font = new System.Drawing.Font("Microsoft Sans Serif", 24F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			}
			this._isX = true;
			this._isGameOver = false;
		}

		private void ClickHandler(object sender, System.EventArgs e)
		{
			Button tempButton = (Button)sender;
			if( this._isGameOver )
			{
				MessageBox.Show("please start new game!","Game End",MessageBoxButtons.OK);
				return;			
			}
			if( tempButton.Text != "" )	
			{
				return;
			}
			if( _isX )	
				tempButton.Text = "X";
			else
				tempButton.Text = "O";
			_isX = !_isX;	
			this._isGameOver = result.CheckWinner(_buttonArray );
		}

        private void openFileDialog1_FileOk(object sender, CancelEventArgs e)
        {

        }
	}
	public class result
	{
		static private int [,] Winners = new int[,]
				   {
						{0,1,2},
						{3,4,5},
						{6,7,8},
						{0,3,6},
						{1,4,7},
						{2,5,8},
						{0,4,8},
						{2,4,6}
				   };
		static public bool CheckWinner( Button [] myControls )
		{
			bool gameOver = false;
			for(int i = 0; i < 8; i++)
			{
				int a = Winners[i,0],b=Winners[i,1],c=Winners[i,2];		
				Button b1=myControls[a], b2=myControls[b], b3=myControls[c];
				if(b1.Text == "" || b2.Text == "" || b3.Text == "" )	
					continue;											
				if(b1.Text == b2.Text && b2.Text == b3.Text )			
				{														
					b1.BackColor = b2.BackColor = b3.BackColor = Color.LightCoral;
					b1.Font = b2.Font = b3.Font = new System.Drawing.Font("Microsoft Sans Serif", 32F, System.Drawing.FontStyle.Italic & System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
					gameOver = true;
					MessageBox.Show(b1.Text + " .... Wins the game!","Game End",MessageBoxButtons.OK);
                    MessageBox.Show("1. The IDE creates most code for the programmer which allows them work faster also it allows them visually look at their work." + 
                       "2.One of the biggest advantages of using and IDE compared to using a plain text editor is the IDE writes code for you when you add certain aspects to it, it you used a plain text editor you would have to create everything from scratch which would take a lot of time. The disadvantages for using an IDE is control with a plain text editor you have control over every aspect of the code with an IDE you have to use the built in functions which take away certain control for the programmers." 
                       + "3. I think it's pretty similar both can do many things for event handling." 
                       + "4. C# and Java both allow only single inheritance of classes. " + "C# and Java both have No Global Methods"
                       +"C# and Java both use wrappers for boxing" 
                       + "Diffrences"
                       + "Verbatim Strings - backslashes, tabs, quotes and newlines can be part of a string without using escape sequences"
                       + "Overflow Detection - the option to explicitly detect or ignore overflow conditions in expressions and type conversions.");
					break;  
				}
			}	
			return gameOver;
		}
        public class Computer
        {
            private string computerPiece = "n";

            //METHODS
            //Move takes a multidimensional array board refernce and a string piece reference
            public void Move(ref string[,] board, ref string cPiece)
            {
                //create an instance of the random class
                Random RandomClass = new Random();
                computerPiece = cPiece;
                bool turnOver = false;
                int rows = 3;
                int columns = 3;

                //AI
                //The computer will make a move based on random numbers
                do
                {

                    int availabilityCounter = 0;

                    ///Check the board to see if any moves are available
                    for (int i = 0; i < rows; i++)
                    {
                        for (int j = 0; j < columns; j++)
                        {

                            if ((board[i, j] == "o") || (board[i, j] == "x"))
                            {
                                availabilityCounter++;
                                if (availabilityCounter == 9)
                                {
                                    turnOver = true;
                                }
                            }
                        }
                    }

                    if (!turnOver)
                    {
                        int randomOne = RandomClass.Next(0, 3);
                        int randomTwo = RandomClass.Next(0, 3);

                        //End turn when the computer places a piece
                        if ((board[randomOne, randomTwo] != "x") & (board[randomOne, randomTwo] != "o"))
                        {
                            board[randomOne, randomTwo] = cPiece;

                            turnOver = true;
                        }
                    }

                } while ((!turnOver));

            }//End Move

        }//End Computer 

    }
}

OK,
To use the method you have written at the bottom you must reproduce the text from your buttons into a 2d string array.
make a new method to handle the conversion.
You can use Button.Text to get the text displayed on the button.
You can use String.ToLower to convert the text to lower caase (as that is how your Computer class is written)
Remember that an array will start at 0 rather than 1 like your button naming convention (making [2,2] the equivelent of button 9 for instance).
That should be all you need, Have Fun!

This article has been dead for over six months. Start a new discussion instead.