• Advertisement
Sign in to follow this  
  • entries
    246
  • comments
    175
  • views
    153935

Tic Tac Toe source

Sign in to follow this  

165 views

Drakkcon requested i post my source. cool. download it here. sorry about the lack of commenting, i can add that in if its a problem.

here is one of the files pre-generated by vc# express. it contains just Main, and i didn't touch it.


#region Using directives

using System;
using System.Collections.Generic;
using System.Windows.Forms;

#endregion

namespace tic_tac_toe
{
static class Program
{
///
/// The main entry point for the application.
///

[STAThread]
static void Main()
{
Application.EnableVisualstyles();
Application.EnableRTLMirroring();
Application.Run(new Form1());
}
}
}


here we have the designer information. all of this was also pre-generated by vc# express, i used the visual editor to place and alter the objects on the form.

[code lang="c#"]
namespace tic_tac_toe
{
partial class Form1
{
///
/// Required designer variable.
///

private System.ComponentModel.IContainer components = null;

///
/// Clean up any resources being used.
///

protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}

#region Windows Form Designer generated code

///
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
///

private void InitializeComponent()
{
this.boardLabelTemplate = new System.Windows.Forms.Label();
this.playerNameTextbox = new System.Windows.Forms.TextBox();
this.menuStrip1 = new System.Windows.Forms.MenuStrip();
this.newGameToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.asXToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.asOToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.aboutToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.leftRaftingContainer = new System.Windows.Forms.RaftingContainer();
this.leftRaftingContainer1 = new System.Windows.Forms.RaftingContainer();
this.topRaftingContainer = new System.Windows.Forms.RaftingContainer();
this.bottomRaftingContainer = new System.Windows.Forms.RaftingContainer();
this.playerSymbolLabel = new System.Windows.Forms.Label();
this.menuStrip1.SuspendLayout();
((System.ComponentModel.ISupportInitialize)(this.leftRaftingContainer)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.leftRaftingContainer1)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.topRaftingContainer)).BeginInit();
this.topRaftingContainer.SuspendLayout();
((System.ComponentModel.ISupportInitialize)(this.bottomRaftingContainer)).BeginInit();
this.SuspendLayout();
//
// boardLabelTemplate
//
this.boardLabelTemplate.BackColor = System.Drawing.SystemColors.ButtonFace;
this.boardLabelTemplate.Borderstyle = System.Windows.Forms.Borderstyle.FixedSingle;
this.boardLabelTemplate.Font = new System.Drawing.Font("Arial Black", 48F, System.Drawing.Fontstyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.boardLabelTemplate.Location = new System.Drawing.Point(13, 34);
this.boardLabelTemplate.Name = "boardLabelTemplate";
this.boardLabelTemplate.Size = new System.Drawing.Size(60, 60);
this.boardLabelTemplate.TabIndex = 0;
this.boardLabelTemplate.Text = "O";
this.boardLabelTemplate.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
//
// playerNameTextbox
//
this.playerNameTextbox.AutoSize = false;
this.playerNameTextbox.Font = new System.Drawing.Font("Arial Black", 14.25F, System.Drawing.Fontstyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.playerNameTextbox.Location = new System.Drawing.Point(51, 225);
this.playerNameTextbox.MaxLength = 10;
this.playerNameTextbox.Name = "playerNameTextbox";
this.playerNameTextbox.Size = new System.Drawing.Size(143, 35);
this.playerNameTextbox.TabIndex = 1;
this.playerNameTextbox.TabStop = false;
this.playerNameTextbox.Text = "PlayerName";
//
// menuStrip1
//
this.menuStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.newGameToolStripMenuItem,
this.aboutToolStripMenuItem});
this.menuStrip1.Location = new System.Drawing.Point(0, 0);
this.menuStrip1.Name = "menuStrip1";
this.menuStrip1.Padding = new System.Windows.Forms.Padding(6, 2, 0, 2);
this.menuStrip1.Raft = System.Windows.Forms.RaftingSides.Top;
this.menuStrip1.TabIndex = 0;
this.menuStrip1.Text = "menuStrip1";
//
// newGameToolStripMenuItem
//
this.newGameToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.asXToolStripMenuItem,
this.asOToolStripMenuItem});
this.newGameToolStripMenuItem.Name = "newGameToolStripMenuItem";
this.newGameToolStripMenuItem.SettingsKey = "Form1.newGameToolStripMenuItem";
this.newGameToolStripMenuItem.Text = "New Game";
//
// asXToolStripMenuItem
//
this.asXToolStripMenuItem.Name = "asXToolStripMenuItem";
this.asXToolStripMenuItem.SettingsKey = "Form1.asXToolStripMenuItem";
this.asXToolStripMenuItem.Text = "As X";
this.asXToolStripMenuItem.Click += new System.EventHandler(this.asXToolStripMenuItem_Click);
//
// asOToolStripMenuItem
//
this.asOToolStripMenuItem.Name = "asOToolStripMenuItem";
this.asOToolStripMenuItem.SettingsKey = "Form1.asOToolStripMenuItem";
this.asOToolStripMenuItem.Text = "As O";
this.asOToolStripMenuItem.Click += new System.EventHandler(this.asOToolStripMenuItem_Click);
//
// aboutToolStripMenuItem
//
this.aboutToolStripMenuItem.Name = "aboutToolStripMenuItem";
this.aboutToolStripMenuItem.SettingsKey = "Form1.aboutToolStripMenuItem";
this.aboutToolStripMenuItem.Text = "About";
this.aboutToolStripMenuItem.Click += new System.EventHandler(this.aboutToolStripMenuItem_Click);
//
// leftRaftingContainer
//
this.leftRaftingContainer.Dock = System.Windows.Forms.Dockstyle.Left;
this.leftRaftingContainer.Name = "leftRaftingContainer";
//
// leftRaftingContainer1
//
this.leftRaftingContainer1.Dock = System.Windows.Forms.Dockstyle.Left;
this.leftRaftingContainer1.Name = "leftRaftingContainer1";
//
// topRaftingContainer
//
this.topRaftingContainer.Controls.Add(this.menuStrip1);
this.topRaftingContainer.Dock = System.Windows.Forms.Dockstyle.Top;
this.topRaftingContainer.Name = "topRaftingContainer";
//
// bottomRaftingContainer
//
this.bottomRaftingContainer.Dock = System.Windows.Forms.Dockstyle.Bottom;
this.bottomRaftingContainer.Name = "bottomRaftingContainer";
//
// playerSymbolLabel
//
this.playerSymbolLabel.Font = new System.Drawing.Font("Arial Black", 20.25F, System.Drawing.Fontstyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.playerSymbolLabel.Location = new System.Drawing.Point(13, 225);
this.playerSymbolLabel.Name = "playerSymbolLabel";
this.playerSymbolLabel.Size = new System.Drawing.Size(31, 35);
this.playerSymbolLabel.TabIndex = 6;
this.playerSymbolLabel.Text = "X";
this.playerSymbolLabel.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(192)))), ((int)(((byte)(192)))), ((int)(((byte)(255)))));
this.ClientSize = new System.Drawing.Size(206, 272);
this.Controls.Add(this.playerSymbolLabel);
this.Controls.Add(this.playerNameTextbox);
this.Controls.Add(this.boardLabelTemplate);
this.Controls.Add(this.leftRaftingContainer);
this.Controls.Add(this.leftRaftingContainer1);
this.Controls.Add(this.topRaftingContainer);
this.Controls.Add(this.bottomRaftingContainer);
this.FormBorderstyle = System.Windows.Forms.FormBorderstyle.FixedDialog;
this.MaximizeBox = false;
this.Name = "Form1";
this.Text = "Tic Tac Toe";
this.Load += new System.EventHandler(this.Form1_Load);
this.menuStrip1.ResumeLayout(false);
((System.ComponentModel.ISupportInitialize)(this.leftRaftingContainer)).EndInit();
((System.ComponentModel.ISupportInitialize)(this.leftRaftingContainer1)).EndInit();
((System.ComponentModel.ISupportInitialize)(this.topRaftingContainer)).EndInit();
this.topRaftingContainer.ResumeLayout(false);
this.topRaftingContainer.PerformLayout();
((System.ComponentModel.ISupportInitialize)(this.bottomRaftingContainer)).EndInit();
this.ResumeLayout(false);
this.PerformLayout();

}

#endregion

private System.Windows.Forms.Label boardLabelTemplate;
private System.Windows.Forms.TextBox playerNameTextbox;
private System.Windows.Forms.MenuStrip menuStrip1;
private System.Windows.Forms.RaftingContainer leftRaftingContainer;
private System.Windows.Forms.RaftingContainer leftRaftingContainer1;
private System.Windows.Forms.RaftingContainer topRaftingContainer;
private System.Windows.Forms.RaftingContainer bottomRaftingContainer;
private System.Windows.Forms.ToolStripMenuItem aboutToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem newGameToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem asXToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem asOToolStripMenuItem;
private System.Windows.Forms.Label playerSymbolLabel;
}
}


and now we have the meat of the program. see, i don't need any commenting to this. it reads very well to me. the only thing that might need some commenting is the stuff i do in MainForm_Load. oh wells, here we go..

[code lang="c#"]
#region Using directives

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

#endregion

namespace tic_tac_toe
{
partial class Form1 : Form
{
public Label[,] boardLabel = null;
public Random randomGenerator = null;

string PlayerSymbol, ComputerAISymbol;

public Form1()
{
InitializeComponent();
randomGenerator = new Random (Environment.TickCount);
}

private void Form1_Load(object sender, EventArgs e)
{
boardLabel = new Label[3, 3];
for (int x = 0; x < 3; x++)
{
for (int y = 0; y < 3; y++)
{
boardLabel[x, y] = new Label();
boardLabel[x, y].BackColor = boardLabelTemplate.BackColor;
boardLabel[x, y].Borderstyle = boardLabelTemplate.Borderstyle;
boardLabel[x, y].Font = boardLabelTemplate.Font;
boardLabel[x, y].Location = new Point(boardLabelTemplate.Location.X + x * boardLabelTemplate.Size.Width,
boardLabelTemplate.Location.Y + y * boardLabelTemplate.Size.Height);
boardLabel[x, y].Size = boardLabelTemplate.Size;
boardLabel[x, y].TextAlign = boardLabelTemplate.TextAlign;
boardLabel[x, y].Click += new EventHandler(boardLabel_Click);
Controls.Add (boardLabel[x, y]);
}
}
boardLabelTemplate.Hide();

ClearBoard();

PlayerSymbol = "X";
ComputerAISymbol = "O";
}

public bool computerAIRunning = false;

private void boardLabel_Click(object sender, EventArgs e)
{
Label clickedLabel = (Label)sender;

if (!computerAIRunning)
if (clickedLabel.Text == "")
{
clickedLabel.Text = PlayerSymbol;
clickedLabel.Refresh();
if (CheckForWin(PlayerSymbol) == true)
{
MessageBox.Show(playerNameTextbox.Text + " wins!", "Game Over");
ClearBoard();
}
else if (CheckForTie() == true)
{
MessageBox.Show("Tied!", "Game Over");
ClearBoard();
}
else
{
// computer's turn!
computerAIRunning = true;
Wait(0.75);
RunComputerAI();
if (CheckForWin(ComputerAISymbol) == true)
{
MessageBox.Show("Computer AI wins..", "Game Over");
ClearBoard();
}
else if (CheckForTie() == true)
{
MessageBox.Show("Tied!", "Game Over");
ClearBoard();
}
computerAIRunning = false;
}
}
}

public void Wait(double secondsToWait)
{
DateTime startWaitTime = DateTime.Now;
while (((TimeSpan)(DateTime.Now - startWaitTime)).TotalSeconds < secondsToWait) ;
}

public void ClearBoard()
{
if (boardLabel == null) return;
for (int x = 0; x < 3; x++)
{
for (int y = 0; y < 3; y++)
{
if (boardLabel[x, y] != null)
boardLabel[x, y].Text = "";
}
}
}

public bool CheckForWin(string symbol)
{
// check horizontal lines first
for (int y = 0; y < 3; y++)
{
if (boardLabel[0, y].Text == symbol &&
boardLabel[1, y].Text == symbol &&
boardLabel[2, y].Text == symbol)
{
return true;
}
}

// check vertical lines next
for (int x = 0; x < 3; x++)
{
if (boardLabel[x, 0].Text == symbol &&
boardLabel[x, 1].Text == symbol &&
boardLabel[x, 2].Text == symbol)
{
return true;
}
}

// check corner-to-corner
if (boardLabel[0, 0].Text == symbol &&
boardLabel[1, 1].Text == symbol &&
boardLabel[2, 2].Text == symbol)
return true;
if (boardLabel[0, 2].Text == symbol &&
boardLabel[1, 1].Text == symbol &&
boardLabel[2, 0].Text == symbol)
return true;

// still here? didn't find a win
return false;
}

public void RunComputerAI()
{
int x, y;

x = randomGenerator.Next(3);
y = randomGenerator.Next(3);

while (boardLabel[x, y].Text != "")
{
x = randomGenerator.Next(3);
y = randomGenerator.Next(3);
}

boardLabel[x, y].Text = ComputerAISymbol;
boardLabel[x, y].Refresh();
}

public bool CheckForTie()
{
for (int x = 0; x < 3; x++)
{
for (int y = 0; y < 3; y++)
{
if (boardLabel[x, y].Text == "") return false;
}
}
return true;
}

private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
{
MessageBox.Show("SteelGolem's Tic Tac Toe" + Environment.NewLine + "AI version: v0.stupid", "About");
}

private void asXToolStripMenuItem_Click(object sender, EventArgs e)
{
playerSymbolLabel.Text = PlayerSymbol = "X";
ComputerAISymbol = "O";
ClearBoard();
}

private void asOToolStripMenuItem_Click(object sender, EventArgs e)
{
playerSymbolLabel.Text = PlayerSymbol = "O";
ComputerAISymbol = "X";
ClearBoard();
}
}
}

Sign in to follow this  


0 Comments


Recommended Comments

There are no comments to display.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Advertisement