0% found this document useful (0 votes)
23 views

Player Computerplayer Player: "Score"

This document defines classes for players in a tic-tac-toe game. It includes a Player class with properties for the player's mark and score. It also defines subclasses for a NamedPlayer that stores the player's name, and a ComputerPlayer that randomly selects moves. The document also includes code for a Form1 class that controls the tic-tac-toe game logic and user interface.

Uploaded by

Umme Laila
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views

Player Computerplayer Player: "Score"

This document defines classes for players in a tic-tac-toe game. It includes a Player class with properties for the player's mark and score. It also defines subclasses for a NamedPlayer that stores the player's name, and a ComputerPlayer that randomly selects moves. The document also includes code for a Form1 class that controls the tic-tac-toe game logic and user interface.

Uploaded by

Umme Laila
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

namespace WindowsFormsApplication2 {

{ class ComputerPlayer:Player
public class Player {
{ private char mark; public ComputerPlayer(char
private int score; mark):base(mark) {
}
public Player(char m) {
mark = m; public int getMove() {
score = 0; Random rnd = new Random();
} int i= rnd.Next(0, 9);
public char Mark // MessageBox.Show(i.ToString());
{ return i; } }}
get
{ return mark; }
}
using System;
public int Score using System.Collections.Generic;
{ get{ using System.ComponentModel;
return score; using System.Data;
} using System.Drawing;
set{ score += value ; using System.Linq;
Console.WriteLine("Score" + score); using System.Text;
} } }} using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApplication2 using Microsoft.VisualBasic;
{
class NamedPlayer:Player namespace WindowsFormsApplication2
{ string name; {
public partial class Form1 : Form
public NamedPlayer(char {
mark):base(mark) { private int moves;
name = public Player p1;
Microsoft.VisualBasic.Interaction.InputBox( public Player p2;
"Enter your Name", "Named Dialogue Box", Player CurrentPlayer;
"Default", -1, -1); Button[] _buttonArray;
} public Form1()
public char Mark {
{ get InitializeComponent();
{ return base.Mark; moves = 0;
} } _buttonArray = new Button[9] {
public string Name { get { return name; } btn_0, btn_1, btn_2, btn_3, btn_4, btn_5,
} } btn_6, btn_7, btn_8 };
} this.btn_reset_Click(this, null);
namespace WindowsFormsApplication2 this.BackColor = Color.Black;
} if (moves > 7) break;

private void button1_Click_1(object } while


sender, EventArgs e) (!(_buttonArray[a].Text.Equals("-")));
{ ButtonClick(sender); }
_buttonArray[a].Text =
private void button5_Click(object CurrentPlayer.Mark.ToString();
sender, EventArgs e) _buttonArray[a].Enabled = false;
{ moves++;
this.btn_reset_Click(this, null); ResultTic();
p1 = new NamedPlayer('O');
p2 = new ComputerPlayer('X');
moves = 0; }
lb_name.Text = "Welcome " +
((NamedPlayer)(p1)).Name; public void Score()
lb_user1.Text = lb_user1.Text + "( " + {
((NamedPlayer)(p1)).Name + ")"; }
if (CurrentPlayer is NamedPlayer)
private void btn_reset_Click(object {
sender, EventArgs e){
foreach (Button b in _buttonArray) CurrentPlayer.Score = 1;
{ b.Text = "-"; txt_userscore.Text =
b.Enabled = true; CurrentPlayer.Score.ToString();
} }
moves = 0; else
} {

CurrentPlayer.Score = 1;
public Player getNextPlayer() txt_compScore.Text =
{ if (moves % 2 == 0) CurrentPlayer.Score.ToString();
{ return p1;
}
else { }
return p2; } }
}

public void ComputerMov(){ public void ResultTic(){


CurrentPlayer = this.getNextPlayer(); // Horizontal Win Check
int a;
do if(moves>=5){
{ for(int i=0;i<9;i+=3){
a=
((ComputerPlayer)(CurrentPlayer)).getMove
();
[i+3].Text)&&_buttonArray[i+3].Text.Equals
if(_buttonArray[i].Text.Equals(_buttonArray (_buttonArray[i+6].Text)&&
[i+1].Text)&&_buttonArray[i+1].Text.Equals _buttonArray[i].Text!="-")
(_buttonArray[i+2].Text)&&
_buttonArray[i].Text!="-")
{ { Score();
Score();
MessageBox.Show(this," horizontal if(!(_buttonArray[i].Text.Equals("-
Player "+_buttonArray[i].Text+" Wins "); "))){
this.btn_reset_Click(this, null); MessageBox.Show(this," vertical
break; Player "+_buttonArray[i].Text+" Wins ");
this.btn_reset_Click(this, null);
// } break;
} }
} }
}
//Diagonal Check
int val=4,j=4;
for(int i=0;i<3;i+=2){ }

if(_buttonArray[i].Text.Equals(_buttonArray }
[val].Text)&&_buttonArray[val].Text.Equals(
_buttonArray[val+j].Text)&& private void btn_0_Click(object sender,
_buttonArray[i].Text!="-") EventArgs e)
{ Score(); {
ButtonClick(sender);
if(!(_buttonArray[i].Text.Equals("-"))){ }
MessageBox.Show(this," Diagonal
Player "+_buttonArray[i].Text+" Wins ");
this.btn_reset_Click(this, null); public void ButtonClick(object sender) {
break; CurrentPlayer = this.getNextPlayer();
} Button b = (Button)sender;
}
j=2; b.Text =
} CurrentPlayer.Mark.ToString();

// Vertical Check moves++;


b.Enabled = false;
ResultTic();
for(int i=0;i<3;i++){ ComputerMov();
}
if(_buttonArray[i].Text.Equals(_buttonArray

You might also like