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

Tic Tac Toe source

Sign in to follow this  

127 views

[edit: screw it. hand editing and use of <pre> seems to be the only way out. sucky, i like the [s0urce] windows.]

[edit: hmm. after converting 'tab' to two spaces, it's still too wide. seems to be a problem with the code generation window. obviously i'm going to have to do this differently.]

[edit: goddamn it! this post is too wide! anyone know how i can fix that?]

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 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..


#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  


1 Comment


Recommended Comments

Thanks, this is very useful!
Form_Load doesn't require commenting, accessing the label in the array through a for loop to edit their properties is a much better method than I was using (Editing one in the form designer and copy-pasting). This probably will lessen the executable size too which I should definately do.

Share this comment


Link to comment

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