Simple Multiplayer Tic Tac Toe game C#

Published  Feb 17, 2014
Updated  Apr 01, 2014
0  1

Description

Player 1 is always X and player 2 is always 0
X always make the first move.
If you want to change names or reset the score click on the reset button.
The board gets autoreset after the match.

Simple Multiplayer Tic Tac Toe Game.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Tic_Tac_Toe
{
    public partial class frmTicTacToe : Form
    {
        public frmTicTacToe()  { InitializeComponent(); }

        bool game;
        string p1Name, p2Name;
        char player = 'X';
        int xScore = 1, oScore = 1;
        List<int> X = new List<int>();
        List<int> O = new List<int>();

       public void setNames() {
            p1Name = InputBox.Input.Show("Enter player1's name", "Input Required","");
            p2Name = InputBox.Input.Show("Enter player2's name", "Input Required", "");
            lblP1Name.Text = p1Name + ":";
            lblP2Name.Text = p2Name + ":";
       }                        

       void check(int getSlotNr) {
          game = true;
          ((PictureBox)this.Controls["pbSlot" + getSlotNr.ToString()]).Image = (Image)Properties.Resources.ResourceManager.GetObject(player.ToString());
          ((PictureBox)this.Controls["pbSlot" + getSlotNr.ToString()]).Enabled = false;
          if (player == 'X') X.Add(getSlotNr);
          else O.Add(getSlotNr);                            
          result();
          if(game == true) player = (player == 'X') ? 'O' : 'X';
       }

        public void result() {
            int[,] win = new int[,] {
                   {1,2,3}, {4,5,6}, {7,8,9},
                   {1,4,7}, {2,5,8}, {3,6,9},
                   {1,5,9}, {7,5,3}
            }; 

            List<int>[] p = new List<int>[] { X, O };
            int listPos = (player == 'X') ? 0 : 1;

            for (int i = 0; i < 8; i++) {
                if (p[listPos].Contains(win[i, 0]) && p[listPos].Contains(win[i, 1]) && p[listPos].Contains(win[i, 2])) {
                    ((Label)this.Controls["lblP" + player.ToString() + "Score"]).Text = ((player == 'X') ? xScore++ : oScore++).ToString();
                    string winner = (player == 'X') ? p1Name  : p2Name;
                    MessageBox.Show(winner + " won!", "Result");
                    clearBoard();
                }
            }

            if (X.Count + O.Count == 9) {
               MessageBox.Show("It's a draw!", "Result");
               clearBoard();                
            }
        }

        void clearBoard() {
            for (int i = 1; i <= 9; i++) {
                ((PictureBox)this.Controls["pbSlot" + i.ToString()]).Image = (Image)Properties.Resources.Slots;
                ((PictureBox)this.Controls["pbSlot" + i.ToString()]).Enabled = true;
            }
            game = false;
            player = 'X';
            X.Clear();
            O.Clear();
        }

        private void pbSlot1_Click(object sender, EventArgs e) { check(1); }
        private void pbSlot2_Click(object sender, EventArgs e) { check(2); }
        private void pbSlot3_Click(object sender, EventArgs e) { check(3); }
        private void pbSlot4_Click(object sender, EventArgs e) { check(4); }
        private void pbSlot5_Click(object sender, EventArgs e) { check(5); }
        private void pbSlot6_Click(object sender, EventArgs e) { check(6); }
        private void pbSlot7_Click(object sender, EventArgs e) { check(7); }
        private void pbSlot8_Click(object sender, EventArgs e) { check(8); }
        private void pbSlot9_Click(object sender, EventArgs e) { check(9); }

        private void btnReset_Click(object sender, EventArgs e) {
            clearBoard();
            setNames();
            lblPXScore.Text = "0";
            lblPOScore.Text = "0";
            xScore = 1;
            oScore = 1;
        }

        private void frmTicTacToe_Load(object sender, EventArgs e) { setNames(); }    
    }
}

Screenshots

Download

Filename
Size
Date
Downloads
39.83 KB
Feb 17, 2014
98

Comments

Sign in to comment.
Akwayiyo   -  Mar 08, 2015

the slots and ? give problems

 Respond  
Are you sure you want to unfollow this person?
Are you sure you want to delete this?
Click "Unsubscribe" to stop receiving notices pertaining to this post.
Click "Subscribe" to resume notices pertaining to this post.