Tic Tac Toe Simulator
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2

Thread: Tic Tac Toe Simulator

  1. #1
    Join Date
    Nov 2011
    Posts
    1

    Tic Tac Toe Simulator

    Hello, I have to do an assignment for my class which asks:

    "Create an application that simulates a game of tic-tac-toe. The application should use a two-dimensional int array to simulate the game boar in memory. When the user clicks the New Game button, the application should step through the array, storing a random number in the range of 0 through 1 in each element. The number 0 represents O and the number 1 represents X. The form should then be updated to display the game board. The application should display a message indicating whether player X won, player Y won, or the game was a tie."

    Now, I was dumb and didn't read the entire question and just created a program that lets you play tic tac toe instead of simulating it. Can someone help me modify my code to fit the criteria of the problem above?

    My code:


    Code:
    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.GroupBox groupBox1;
    		private System.Windows.Forms.Button btn0;
    		private System.Windows.Forms.Button btn1;
    		private System.Windows.Forms.Button btn2;
    		private System.Windows.Forms.Button btn3;
    		private System.Windows.Forms.Button btn4;
    		private System.Windows.Forms.Button btn5;
    		private System.Windows.Forms.Button btn6;
    		private System.Windows.Forms.Button btn7;
    		private System.Windows.Forms.Button btn8;
    		/// <summary>
    		/// Required designer variable.
    		/// </summary>
    		private System.ComponentModel.Container components = null;
    		enum PlayerTurn {No, Yes};
    		PlayerTurn IsPlayer1  = PlayerTurn.Yes;
    		private System.Windows.Forms.Button button1;
    		private System.Windows.Forms.Button button2;
    		private System.Windows.Forms.Label label1;
    		PlayerTurn IsPlayer2  = PlayerTurn.No;
    
    		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.groupBox1 = new System.Windows.Forms.GroupBox();
    			this.button1 = new System.Windows.Forms.Button();
    			this.btn8 = new System.Windows.Forms.Button();
    			this.btn7 = new System.Windows.Forms.Button();
    			this.btn6 = new System.Windows.Forms.Button();
    			this.btn5 = new System.Windows.Forms.Button();
    			this.btn4 = new System.Windows.Forms.Button();
    			this.btn3 = new System.Windows.Forms.Button();
    			this.btn2 = new System.Windows.Forms.Button();
    			this.btn1 = new System.Windows.Forms.Button();
    			this.btn0 = new System.Windows.Forms.Button();
    			this.button2 = new System.Windows.Forms.Button();
    			this.label1 = new System.Windows.Forms.Label();
    			this.groupBox1.SuspendLayout();
    			this.SuspendLayout();
    			// 
    			// groupBox1
    			// 
    			this.groupBox1.Controls.Add(this.button2);
    			this.groupBox1.Controls.Add(this.label1);
    			this.groupBox1.Controls.Add(this.button1);
    			this.groupBox1.Controls.Add(this.btn8);
    			this.groupBox1.Controls.Add(this.btn7);
    			this.groupBox1.Controls.Add(this.btn6);
    			this.groupBox1.Controls.Add(this.btn5);
    			this.groupBox1.Controls.Add(this.btn4);
    			this.groupBox1.Controls.Add(this.btn3);
    			this.groupBox1.Controls.Add(this.btn2);
    			this.groupBox1.Controls.Add(this.btn1);
    			this.groupBox1.Controls.Add(this.btn0);
    			this.groupBox1.Location = new System.Drawing.Point(88, 24);
    			this.groupBox1.Name = "groupBox1";
    			this.groupBox1.Size = new System.Drawing.Size(360, 256);
    			this.groupBox1.TabIndex = 0;
    			this.groupBox1.TabStop = false;
    			this.groupBox1.Text = "Tic Tac Toe";
    			// 
    			// button1
    			// 
    			this.button1.Location = new System.Drawing.Point(248, 184);
    			this.button1.Name = "button1";
    			this.button1.Size = new System.Drawing.Size(88, 23);
    			this.button1.TabIndex = 9;
    			this.button1.Text = "&Reset Game";
    			this.button1.Click += new System.EventHandler(this.button1_Click);
    			// 
    			// btn8
    			// 
    			this.btn8.Location = new System.Drawing.Point(128, 128);
    			this.btn8.Name = "btn8";
    			this.btn8.Size = new System.Drawing.Size(32, 32);
    			this.btn8.TabIndex = 8;
    			this.btn8.Click += new System.EventHandler(this.btn8_Click);
    			// 
    			// btn7
    			// 
    			this.btn7.Location = new System.Drawing.Point(80, 128);
    			this.btn7.Name = "btn7";
    			this.btn7.Size = new System.Drawing.Size(32, 32);
    			this.btn7.TabIndex = 7;
    			this.btn7.Click += new System.EventHandler(this.btn7_Click);
    			// 
    			// btn6
    			// 
    			this.btn6.Location = new System.Drawing.Point(32, 128);
    			this.btn6.Name = "btn6";
    			this.btn6.Size = new System.Drawing.Size(32, 32);
    			this.btn6.TabIndex = 6;
    			this.btn6.Click += new System.EventHandler(this.btn6_Click);
    			// 
    			// btn5
    			// 
    			this.btn5.Location = new System.Drawing.Point(128, 80);
    			this.btn5.Name = "btn5";
    			this.btn5.Size = new System.Drawing.Size(32, 32);
    			this.btn5.TabIndex = 5;
    			this.btn5.Click += new System.EventHandler(this.btn5_Click);
    			// 
    			// btn4
    			// 
    			this.btn4.Location = new System.Drawing.Point(80, 80);
    			this.btn4.Name = "btn4";
    			this.btn4.Size = new System.Drawing.Size(32, 32);
    			this.btn4.TabIndex = 4;
    			this.btn4.Click += new System.EventHandler(this.btn4_Click);
    			// 
    			// btn3
    			// 
    			this.btn3.Location = new System.Drawing.Point(32, 80);
    			this.btn3.Name = "btn3";
    			this.btn3.Size = new System.Drawing.Size(32, 32);
    			this.btn3.TabIndex = 3;
    			this.btn3.Click += new System.EventHandler(this.btn3_Click);
    			// 
    			// btn2
    			// 
    			this.btn2.Location = new System.Drawing.Point(128, 32);
    			this.btn2.Name = "btn2";
    			this.btn2.Size = new System.Drawing.Size(32, 32);
    			this.btn2.TabIndex = 2;
    			this.btn2.Click += new System.EventHandler(this.btn2_Click);
    			// 
    			// btn1
    			// 
    			this.btn1.Location = new System.Drawing.Point(80, 32);
    			this.btn1.Name = "btn1";
    			this.btn1.Size = new System.Drawing.Size(32, 32);
    			this.btn1.TabIndex = 1;
    			this.btn1.Click += new System.EventHandler(this.btn1_Click);
    			// 
    			// btn0
    			// 
    			this.btn0.Location = new System.Drawing.Point(32, 32);
    			this.btn0.Name = "btn0";
    			this.btn0.Size = new System.Drawing.Size(32, 32);
    			this.btn0.TabIndex = 0;
    			this.btn0.Click += new System.EventHandler(this.btn0_Click);
    			// 
    			// button2
    			// 
    			this.button2.Location = new System.Drawing.Point(248, 216);
    			this.button2.Name = "button2";
    			this.button2.Size = new System.Drawing.Size(88, 23);
    			this.button2.TabIndex = 12;
    			this.button2.Text = "&Exit";
    			this.button2.Click += new System.EventHandler(this.button2_Click);
    			// 
    			// label1
    			// 
    			this.label1.Location = new System.Drawing.Point(32, 200);
    			this.label1.Name = "label1";
    			this.label1.Size = new System.Drawing.Size(144, 24);
    			this.label1.TabIndex = 10;
    			this.label1.Text = "Player 1 Turn (0 Sign)";
    			// 
    			// Form1
    			// 
    			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
    			this.ClientSize = new System.Drawing.Size(536, 349);
    			this.Controls.Add(this.groupBox1);
    			this.MaximizeBox = false;
    			this.Name = "Form1";
    			this.Text = "Game";
    			this.groupBox1.ResumeLayout(false);
    			this.ResumeLayout(false);
    
    		}
    		#endregion
    
    		/// <summary>
    		/// The main entry point for the application.
    		/// </summary>
    		[STAThread]
    		static void Main() 
    		{
    			Application.Run(new Form1());
    		}
    		private void SetValue(ref Button oButton)
    		{
    			if ( oButton.Text.Length == 0 ) 
    			{
    				if ( this.IsPlayer1 == PlayerTurn.Yes )
    				{
    					oButton.Text = "0";
    					this.IsPlayer1 = PlayerTurn.No;
    					this.IsPlayer2 = PlayerTurn.Yes;
    					oButton.Enabled = false;
    					this.label1.Text = "Player 2 Turn (1 Sign)";
    				}
    				else if ( this.IsPlayer2 == PlayerTurn.Yes )
    				{
    					oButton.Text = "1";
    					this.IsPlayer2 = PlayerTurn.No;
    					this.IsPlayer1 = PlayerTurn.Yes;
    					oButton.Enabled = false;
    					this.label1.Text = "Player 1 Turn (0 Sign)";
    				}
    			}
    			if ( this.CheckWinner() )
    			{
    				if ( this.IsPlayer1 == PlayerTurn.Yes )
    				{
    					MessageBox.Show("Player 2 is the winner!");
    					this.RefreshGame();
    				}
    				else 
    				{
    					MessageBox.Show("Player 1 is the winner!");
    					this.RefreshGame();
    				}
    			}
    		}
    		private bool CheckWinner()
    		{
    			//define the winner logic here
    			int[,] arr = new int[3,3];
    			this.SetArrary(ref arr);
    			bool bHWinnerFound = true;
    			bool bVWinnerFound = true;
    
    			// Check for horizontal and vertical Cross
    			for (int iRow = 0; iRow < 3; iRow++)
    			{
    				bHWinnerFound = true;
    				bVWinnerFound = true;
    				for (int iCol = 0; iCol <2; iCol++ )
    				{
    					if ( arr[iRow,iCol] != arr[iRow,iCol+1] || arr[iRow,iCol] == -1) 
    					{
    						bHWinnerFound = false;
    					}
    					if ( arr[iCol,iRow] != arr[iCol+1,iRow] || arr[iCol,iRow] == -1) 
    					{
    						bVWinnerFound = false;
    					}
    				}
    				if ( bVWinnerFound || bHWinnerFound )
    				{
    					return true;
    					break;
    				}
    			}
    
    			//for diagonal
    			if ( (arr[0,0] == arr[1,1] && arr[1,1] == arr[2,2] && arr[1,1] != -1 ) || 
    					(arr[0,2] == arr[1,1] && arr[1,1] == arr[2,0] && arr[1,1] != -1  )  )
    				return true;
    
    			return false;
    
    		}
    		private void SetArrary(ref int[,] arr)
    		{
    			arr[0,0] = this.btn0.Text.Length == 0 ? -1:int.Parse(this.btn0.Text);
    			arr[0,1] = this.btn1.Text.Length == 0 ? -1:int.Parse(this.btn1.Text);
    			arr[0,2] = this.btn2.Text.Length == 0 ? -1:int.Parse(this.btn2.Text);
    			arr[1,0] = this.btn3.Text.Length == 0 ? -1:int.Parse(this.btn3.Text);
    			arr[1,1] = this.btn4.Text.Length == 0 ? -1:int.Parse(this.btn4.Text);
    			arr[1,2] = this.btn5.Text.Length == 0 ? -1:int.Parse(this.btn5.Text);
    			arr[2,0] = this.btn6.Text.Length == 0 ? -1:int.Parse(this.btn6.Text);
    			arr[2,1] = this.btn7.Text.Length == 0 ? -1:int.Parse(this.btn7.Text);
    			arr[2,2] = this.btn8.Text.Length == 0 ? -1:int.Parse(this.btn8.Text);
    		}
    		private void btn0_Click(object sender, System.EventArgs e)
    		{
    			this.SetValue(ref this.btn0);
    		}
    		private void btn1_Click(object sender, System.EventArgs e)
    		{
    			this.SetValue(ref this.btn1);
    		}
    		private void btn2_Click(object sender, System.EventArgs e)
    		{
    			this.SetValue(ref this.btn2);
    		}
    		private void btn3_Click(object sender, System.EventArgs e)
    		{
    			this.SetValue(ref this.btn3);
    		}
    		private void btn4_Click(object sender, System.EventArgs e)
    		{
    			this.SetValue(ref this.btn4);
    		}
    		private void btn5_Click(object sender, System.EventArgs e)
    		{
    			this.SetValue(ref this.btn5);
    		}
    		private void btn6_Click(object sender, System.EventArgs e)
    		{
    			this.SetValue(ref this.btn6);
    		}
    		private void btn7_Click(object sender, System.EventArgs e)
    		{
    			this.SetValue(ref this.btn7);
    		} 
    		private void btn8_Click(object sender, System.EventArgs e)
    		{
    			this.SetValue(ref this.btn8);
    		}
    		private void RefreshGame()
    		{
    			this.btn0.Text = "";
    			this.btn0.Enabled = true;
    			this.btn1.Text = "";
    			this.btn1.Enabled = true;
    			this.btn2.Text = "";
    			this.btn2.Enabled = true;
    			this.btn3.Text = "";
    			this.btn3.Enabled = true;
    			this.btn4.Text = "";
    			this.btn4.Enabled = true;
    			this.btn5.Text = "";
    			this.btn5.Enabled = true;
    			this.btn6.Text = "";
    			this.btn6.Enabled = true;
    			this.btn7.Text = "";
    			this.btn7.Enabled = true;
    			this.btn8.Text = "";
    			this.btn8.Enabled = true;
    			this.IsPlayer1 = PlayerTurn.Yes;
    			this.IsPlayer2 = PlayerTurn.No;
    			this.label1.Text = "Player 1 Turn (0 Sign)";
    		}
    
    		private void button1_Click(object sender, System.EventArgs e)
    		{
    			this.RefreshGame();
    		}
    
    		private void button2_Click(object sender, System.EventArgs e)
    		{
    			Application.Exit();
    		}
    
    
    	}
    }

  2. #2
    Join Date
    Feb 2011
    Location
    United States
    Posts
    1,006

    Re: Tic Tac Toe Simulator

    If you're skilled enough to write that, it should be a simple matter to modify it. Just disable all your buttons, create a new button ("simulate") that (a) fills the board randomly and (b) calls your method to determine the winner. Output a MessageBox and you win.

    If you have a more specific question, we can help. However, I suspect you are already on the right track.

    EDIT: Also, what's with enum PlayerTurn {No, Yes};? There is a primitive type built in for this called bool (which obviously you know because you use it!)
    Best Regards,

    BioPhysEngr
    http://blog.biophysengr.net
    --
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center