Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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 TicTacToeSimulation {
- public partial class Form1 : Form {
- public Form1() {
- InitializeComponent();
- }
- private void ExitButton_Click(object sender, EventArgs e) {
- Close();
- }
- public int player = 2;
- public int turns = 0;
- public int s1 = 0;
- public int s2 = 0;
- public int sd = 0;
- private void buttonClick(object sender, EventArgs e) {
- Button button = (Button)sender;
- if (button.Text == "")
- {
- if (player % 2 == 0) {
- button.Text = "X";
- player++;
- turns++;
- } else {
- button.Text = "O";
- player++;
- turns++;
- }
- if (CheckDraw() == true) {
- MessageBox.Show("It's A Draw! New Game?");
- sd++;
- NewGame();
- }
- }
- }
- private void Form1_Load(object sender, EventArgs e) {
- XWinsLabel.Text = "X: " + s1;
- OWinsLabel.Text = "O: " + s2;
- DrawsLabel.Text = "Draws: " + sd;
- }
- void NewGame()
- {
- player = 2;
- turns = 0;
- TopLeftButton.Text = TopCenterButton.Text = TopRightButton.Text = CenterLeftButton.Text = CenterButton.Text = CenterRightButton.Text = BottomLeftButton.Text = ButtomCenterButton.Text = ButtonRightButton.Text = "";
- }
- private void NGButton_Click(object sender, EventArgs e) {
- NewGame();
- }
- bool CheckDraw() {
- if (turns == 9)
- return true;
- else
- return false;
- }
- bool CheckWinner() {
- // Vertical
- if ((TopLeftButton.Text == TopCenterButton.Text) && (TopCenterButton.Text == TopRightButton.Text) && TopLeftButton.Text != "")
- return true;
- else if ((CenterLeftButton.Text == CenterButton.Text) && (CenterButton.Text == CenterRightButton.Text) && CenterLeftButton.Text != "")
- return true;
- else if ((BottomLeftButton.Text == ButtomCenterButton.Text) && (ButtomCenterButton.Text == ButtonRightButton.Text) && BottomLeftButton.Text != "")
- return true;
- // Horizontal
- if ((TopLeftButton.Text == CenterLeftButton.Text) && (CenterLeftButton.Text == BottomLeftButton.Text) && TopLeftButton.Text != "")
- return true;
- else if ((TopCenterButton.Text == CenterButton.Text) && (CenterButton.Text == ButtomCenterButton.Text) && TopCenterButton.Text != "")
- return true;
- else if ((TopRightButton.Text == CenterRightButton.Text) && (CenterRightButton.Text == ButtonRightButton.Text) && TopRightButton.Text != "")
- return true;
- // Diagonal
- if ((TopLeftButton.Text == CenterButton.Text) && (CenterButton.Text == ButtonRightButton.Text) && TopLeftButton.Text != "")
- return true;
- else if ((TopRightButton.Text == CenterButton.Text) && (CenterButton.Text == BottomLeftButton.Text) &&)
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement