Jump to content
  • Advertisement
Sign in to follow this  
Drakkcon

Please critique my first game!

This topic is 5087 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I've been a member of these forums for a while and I've been programming for several years, but I hadn't made any games yet. I forced myself to make Tic-Tac-Toe, and I did it with C# (since I'm learning the language) and .net . Click here to download the program! Here is the source code:
[source lang = "c#"]
#region Using directives

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;
using System.Security.Cryptography; //randomizer

#endregion

namespace GDI_Test
{
    partial class Form1 : Form
    {
        private string CompPlayer = "O"; //AI starts as 'oh'
        private string HumanPlayer = "X"; //human player is X by default
        private string Winner = "Nobody"; //current winner
        private string LastPlayer = "O"; //Human goes first by default, as human is X by default
        private bool IsTied = true;

        public Form1()
        {
            InitializeComponent();
        }

        private void QuitApp(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void ResetBoard(object sender, EventArgs e)
        {
            LastPlayer = CompPlayer;

            label1.Text = "_";
            label2.Text = "_";
            label3.Text = "_";
            label4.Text = "_";
            label5.Text = "_";
            label6.Text = "_";
            label7.Text = "_";
            label8.Text = "_";
            label9.Text = "_";

            Winner = "Nobody";
        }

        private void ResetBoard() //this version is not designed to be accessed from the button
        {
            LastPlayer = CompPlayer;

            label1.Text = "_";
            label2.Text = "_";
            label3.Text = "_";
            label4.Text = "_";
            label5.Text = "_";
            label6.Text = "_";
            label7.Text = "_";
            label8.Text = "_";
            label9.Text = "_";

            Winner = "Nobody";
        }

        private void PlayerButtonClicked(object PlayerButton, EventArgs e)
        {
            string PreviousHumanPlayer = HumanPlayer;


            if (PlayerButton == button3)
            {
                HumanPlayer = "X";
                CompPlayer = "O";
            }
            else if (PlayerButton == button2)
            {
                HumanPlayer = "O";
                CompPlayer = "X";
            }

            ResetBoard();
            

        }

        private void CheckForWinner()
        {
            //check to see if there is a winner
            if(label1.Text == HumanPlayer &&
                label2.Text == HumanPlayer &&
                label3.Text == HumanPlayer)
                Winner = "Player";
            if(label1.Text == HumanPlayer &&
                label4.Text == HumanPlayer &&
                label7.Text == HumanPlayer)
                Winner = "Player";
            if(label1.Text == HumanPlayer &&
                label5.Text == HumanPlayer &&
                label9.Text == HumanPlayer)
                Winner = "Player";
            if(label3.Text == HumanPlayer &&
                label6.Text == HumanPlayer &&
                label9.Text == HumanPlayer)
                Winner = "Player";
            if(label4.Text == HumanPlayer &&
                label5.Text == HumanPlayer &&
                label6.Text == HumanPlayer)
                Winner = "Player";
            if(label2.Text == HumanPlayer &&
                label5.Text == HumanPlayer &&
                label8.Text == HumanPlayer)
                Winner = "Player";
            if(label3.Text == HumanPlayer &&
                label5.Text == HumanPlayer &&
                label7.Text == HumanPlayer)
                Winner = "Player";
            if(label7.Text == HumanPlayer &&
                label8.Text == HumanPlayer &&
                label9.Text == HumanPlayer)
                Winner = "Player";

            //checking for AI winner
            if(label1.Text == CompPlayer &&
                label2.Text == CompPlayer &&
                label3.Text == CompPlayer)
                Winner = "AI";
            if(label1.Text == CompPlayer &&
                label4.Text == CompPlayer &&
                label7.Text == CompPlayer)
                Winner = "AI";
            if(label1.Text == CompPlayer &&
                label5.Text == CompPlayer &&
                label9.Text == CompPlayer)
                Winner = "AI";
            if(label3.Text == CompPlayer &&
                label6.Text == CompPlayer &&
                label9.Text == CompPlayer)
                Winner = "AI";
            if(label4.Text == CompPlayer &&
                label5.Text == CompPlayer &&
                label6.Text == CompPlayer)
                Winner = "AI";
            if(label2.Text == CompPlayer &&
                label5.Text == CompPlayer &&
                label8.Text == CompPlayer)
                Winner = "AI";
            if(label3.Text == CompPlayer &&
                label5.Text == CompPlayer &&
                label7.Text == CompPlayer)
                Winner = "AI";
            if(label7.Text == CompPlayer &&
                label8.Text == CompPlayer &&
                label9.Text == CompPlayer)
                Winner = "AI";

            if (Winner == "AI")
            {
                MessageBox.Show("Artificial Intelligence Wins!");
                ResetBoard();
            }
            if (Winner == "Player")
            {
                MessageBox.Show("You Win!");
                ResetBoard();
            }
        }

        private void CheckClick(object o, EventArgs e)
        {
            LabelClicked((System.Windows.Forms.Label)o, e);
        }

        private void AboutButtonClick(object o, EventArgs e)
        {
            MessageBox.Show("Programmed by: Grant Williams \n Year 2004.", 
                            "About");
        }

        private void CheckForTieGame()
        {
            if ((label1.Text == CompPlayer || label1.Text == HumanPlayer) &&
                (label2.Text == CompPlayer || label2.Text == HumanPlayer) &&
                (label3.Text == CompPlayer || label3.Text == HumanPlayer) &&
                (label4.Text == CompPlayer || label4.Text == HumanPlayer) &&
                (label5.Text == CompPlayer || label5.Text == HumanPlayer) &&
                (label6.Text == CompPlayer || label6.Text == HumanPlayer) &&
                (label7.Text == CompPlayer || label7.Text == HumanPlayer) &&
                (label8.Text == CompPlayer || label8.Text == HumanPlayer) &&
                (label9.Text == CompPlayer || label9.Text == HumanPlayer))
                Winner = "Tie";
            if (Winner == "Tie")
            {
                MessageBox.Show("Draw game.");
                ResetBoard();
            }

        }

        private void LabelClicked(Label lbl, EventArgs e)
        {
            Random rand = new Random();

            

            //get input
            if(LastPlayer == CompPlayer)
            {
                if (lbl.Text != CompPlayer && lbl.Text != HumanPlayer)
                {
                    lbl.Text = HumanPlayer;
                    LastPlayer = HumanPlayer;
                }
                else
                {
                    MessageBox.Show("This sector is not empty.");
                }

            }
            CheckForWinner();
            CheckForTieGame();


            //AI
            while (LastPlayer == HumanPlayer)
            {

                
                int RandNumb = rand.Next(9);

                    switch (RandNumb)
                    {
                        case (1):
                            {
                                if (label1.Text == "_")
                                {
                                    label1.Text = CompPlayer;
                                    LastPlayer = CompPlayer;
                                }
                                break;
                            }
                        case (2):
                            {
                                if (label2.Text == "_")
                                {
                                    label2.Text = CompPlayer;
                                    LastPlayer = CompPlayer;
                                }
                                break;
                            }
                        case (3):
                            {
                                if (label3.Text == "_")
                                {
                                    label3.Text = CompPlayer;
                                    LastPlayer = CompPlayer;
                                }
                                break;
                            }
                        case (4):
                            {
                                if (label4.Text == "_")
                                {
                                    label4.Text = CompPlayer;
                                    LastPlayer = CompPlayer;
                                }
                                break;
                            }
                        case (5):
                            {
                                if (label5.Text == "_")
                                {
                                    label5.Text = CompPlayer;
                                    LastPlayer = CompPlayer;
                                }
                                break;
                            }
                        case (6):
                            {
                                if (label6.Text == "_")
                                {
                                    label6.Text = CompPlayer;
                                    LastPlayer = CompPlayer;
                                }
                                break;
                            }
                        case (7):
                            {
                                if (label7.Text == "_")
                                {
                                    label7.Text = CompPlayer;
                                    LastPlayer = CompPlayer;
                                }
                                break;
                            }
                        case (8):
                            {
                                if (label8.Text == "_")
                                {
                                    label8.Text = CompPlayer;
                                    LastPlayer = CompPlayer;
                                }
                                break;
                            }
                        case (9):
                            {
                                if (label9.Text == "_")
                                {
                                    label9.Text = CompPlayer;
                                    LastPlayer = CompPlayer;
                                }
                                break;
                            }
                    }
                

            }
                
                CheckForWinner();
                CheckForTieGame();


        }
    }

}






Here is an image that shows what some of the widget identifiers corrispond to: EDIT:(This isn't a screenshot, just a mapping of the form I used) what do you think! EDIT: For some reason it keeps adding the line: [ source lang="c++"] before the c# one. ______________________________________________ [Edited by - Drakkcon on January 4, 2005 6:35:13 PM]

Share this post


Link to post
Share on other sites
Advertisement
Looks okay to me. Perhaps change "label1" etc in the boxes to "Click Me" so it'll be more obvious what folks should do.

Given any thought to your AI yet? Tic Tac Toe AI is a good start, because you can make it very simple (table lookup) or very complex (minimax tree). There's loads of room for growth.

Share this post


Link to post
Share on other sites
No you misunderstand. That's a form lookup table, not an actual screenshot. It is there so you can look for label1 in the code and know what it corresponds to.
About AI, I'm not sure here. The most I could do is make it to where the computer always wins or ties when going first, I'll see what I can do.

Share this post


Link to post
Share on other sites
no, i think hattan means that when you look at the code, you shouldn't have any labels named label1 unless you're never going to reference it. [edit: try using more descriptive names] oh, and why are you using gdi? theres no reason for any real-time window updates, its all just text. [edit: you're putting much more work into it than it needs!]

[Edited by - SteelGolem on January 4, 2005 7:41:49 PM]

Share this post


Link to post
Share on other sites
I'm not using GDI, that's just the name of the namespace. I was testing out GDI+ on windows forms, and decided to use the form for Tic Tac Toe, notice that there is no GDI used in the code. I renamed the executable of course.

Share this post


Link to post
Share on other sites
Quote:
Original post by SteelGolem
no, i think hattan means that when you look at the code, you shouldn't have any labels named label1 unless you're never going to reference it. [edit: try using more descriptive names]


This is an important point.

You really do need to get in the habit of desctiptively naming EVERYTHING if you are going to use .NET, especially if you use Visual Studio, where you're just dropping new controls etc all over the place.

My first app in VB/ASP.NET is now 75 files and it is a nightmare to debug or change anything.
I used naming conventions in my second one and it is so much easier.

Look here for a good naming standard -

http://www.ssw.com.au/SSW/Standards/DeveloperDotNet/DotNetStandard_ObjectNaming.aspx

Share this post


Link to post
Share on other sites
Pretty cool. Try to work on the AI a little. I have a version that uses a scoring system where certain squares have a higher value than others and an extra value point is added if the player holds an adjacent square. You can approach it lots of different ways. You can download mine if you want. It's in C++.

Tic Tac Toe

Share this post


Link to post
Share on other sites
Alright, thanks for the suggestion virib, I'll take a look!
Thanks cornflake for the link, and I commented on the Tic Tac Toe entry of your journal Steel Golem.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!