I am working on a tic tac toe program in C#. Part of my code is supposed to use if statements that will announce the winner when a player has won. However, when I run the code, it doesn't work. When the player wins, regardless of the combination, the MessageBox never pops up. Does anyone know why? This is my code:
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 TicTacToeV1
{
public partial class Form1 : Form
{
private const string player1 = "Player 1's Turn";
private const string player2 = "Player 2's Turn";
public Form1()
{
InitializeComponent();
}
private void gameBoxPictureBox_Click(object sender, EventArgs e)
{
PictureBox gamePictureBox = (PictureBox)sender;
if(playerTrunLabel.Text == player1)
{
gamePictureBox.Image = Properties.Resources.X_150;
playerTrunLabel.Text = player2;
timeLabel.Text = "10";
}
else
{
gamePictureBox.Image = Properties.Resources.O_150;
playerTrunLabel.Text = player1;
timeLabel.Text = "10";
}
if (gameBox1PictureBox.Image == Properties.Resources.X_150)
{
if (gameBox2PictureBox.Image == Properties.Resources.X_150)
{
if (gameBox3PictureBox.Image == Properties.Resources.X_150)
{
playerTime.Stop();
startButton.Visible = false;
MessageBox.Show("Player 1 wins the game!");
}
}
if (gameBox4PictureBox.Image == Properties.Resources.X_150)
{
if (gameBox7PictureBox.Image == Properties.Resources.X_150)
{
playerTime.Stop();
startButton.Visible = false;
MessageBox.Show("Player 1 wins the game!");
}
}
if (gameBox5PictureBox.Image == Properties.Resources.X_150)
{
if (gameBox9PictureBox.Image == Properties.Resources.X_150)
{
playerTime.Stop();
startButton.Visible = false;
MessageBox.Show("Player 1 wins the game!");
}
}
}
if (gameBox2PictureBox.Image == Properties.Resources.X_150)
{
if (gameBox5PictureBox.Image == Properties.Resources.X_150)
{
if (gameBox8PictureBox.Image == Properties.Resources.X_150)
{
playerTime.Stop();
startButton.Visible = false;
MessageBox.Show("Player 1 wins the game!");
}
}
}
if (gameBox3PictureBox.Image == Properties.Resources.X_150)
{
if (gameBox5PictureBox.Image == Properties.Resources.X_150)
{
if (gameBox7PictureBox.Image == Properties.Resources.X_150)
{
playerTime.Stop();
startButton.Visible = false;
MessageBox.Show("Player 1 wins the game!");
}
}
if (gameBox6PictureBox.Image == Properties.Resources.X_150)
{
if (gameBox9PictureBox.Image == Properties.Resources.X_150)
{
playerTime.Stop();
startButton.Visible = false;
MessageBox.Show("Player 1 wins the game!");
}
}
}
if (gameBox4PictureBox.Image == Properties.Resources.X_150)
{
if (gameBox5PictureBox.Image == Properties.Resources.X_150)
{
if (gameBox6PictureBox.Image == Properties.Resources.X_150)
{
playerTime.Stop();
startButton.Visible = false;
MessageBox.Show("Player 1 wins the game!");
}
}
}
if (gameBox7PictureBox.Image == Properties.Resources.X_150)
{
if (gameBox8PictureBox.Image == Properties.Resources.X_150)
{
if (gameBox9PictureBox.Image == Properties.Resources.X_150)
{
playerTime.Stop();
startButton.Visible = false;
MessageBox.Show("Player 1 wins the game!");
}
}
}
if (gameBox1PictureBox.Image == Properties.Resources.O_150)
{
if (gameBox2PictureBox.Image == Properties.Resources.O_150)
{
if (gameBox3PictureBox.Image == Properties.Resources.O_150)
{
playerTime.Stop();
startButton.Visible = false;
MessageBox.Show("Player 2 wins the game!");
}
}
if (gameBox4PictureBox.Image == Properties.Resources.O_150)
{
if (gameBox7PictureBox.Image == Properties.Resources.O_150)
{
playerTime.Stop();
startButton.Visible = false;
MessageBox.Show("Player 2 wins the game!");
}
}
if (gameBox5PictureBox.Image == Properties.Resources.O_150)
{
if (gameBox9PictureBox.Image == Properties.Resources.O_150)
{
playerTime.Stop();
startButton.Visible = false;
MessageBox.Show("Player 2 wins the game!");
}
}
}
if (gameBox2PictureBox.Image == Properties.Resources.O_150)
{
if (gameBox5PictureBox.Image == Properties.Resources.O_150)
{
if (gameBox8PictureBox.Image == Properties.Resources.O_150)
{
playerTime.Stop();
startButton.Visible = false;
MessageBox.Show("Player 2 wins the game!");
}
}
}
if (gameBox3PictureBox.Image == Properties.Resources.O_150)
{
if (gameBox5PictureBox.Image == Properties.Resources.O_150)
{
if (gameBox7PictureBox.Image == Properties.Resources.O_150)
{
playerTime.Stop();
startButton.Visible = false;
MessageBox.Show("Player 2 wins the game!");
}
}
if (gameBox6PictureBox.Image == Properties.Resources.O_150)
{
if (gameBox9PictureBox.Image == Properties.Resources.O_150)
{
playerTime.Stop();
startButton.Visible = false;
MessageBox.Show("Player 2 wins the game!");
}
}
}
if (gameBox4PictureBox.Image == Properties.Resources.O_150)
{
if (gameBox5PictureBox.Image == Properties.Resources.O_150)
{
if (gameBox6PictureBox.Image == Properties.Resources.O_150)
{
playerTime.Stop();
startButton.Visible = false;
MessageBox.Show("Player 2 wins the game!");
}
}
}
if (gameBox7PictureBox.Image == Properties.Resources.O_150)
{
if (gameBox8PictureBox.Image == Properties.Resources.O_150)
{
if (gameBox9PictureBox.Image == Properties.Resources.O_150)
{
playerTime.Stop();
startButton.Visible = false;
MessageBox.Show("Player 2 wins the game!");
}
}
}
}
private void clearButton_Click(object sender, EventArgs e)
{
gameBox1PictureBox.Image = null;
gameBox2PictureBox.Image = null;
gameBox3PictureBox.Image = null;
gameBox4PictureBox.Image = null;
gameBox5PictureBox.Image = null;
gameBox6PictureBox.Image = null;
gameBox7PictureBox.Image = null;
gameBox8PictureBox.Image = null;
gameBox9PictureBox.Image = null;
startButton.Visible = true;
playerTrunLabel.Text = player1;
playerTime.Start();
timeLabel.Text = "10";
}
private void exitButton_Click(object sender, EventArgs e)
{
this.Close();
}
private void startButton_Click(object sender, EventArgs e)
{
playerTime.Start();
}
private void playerTime_Tick(object sender, EventArgs e)
{
int time = int.Parse(timeLabel.Text);
timeLabel.Text = (--time).ToString();
if (timeLabel.Text == "0")
{
timeLabel.Text = "10";
if (playerTrunLabel.Text == player1)
{
playerTrunLabel.Text = player2;
}
else
{
playerTrunLabel.Text = player1;
}
}
}
}
}
Aucun commentaire:
Enregistrer un commentaire