Hamad Ali

Hamad Ali

  • NA
  • 6
  • 1.7k

C# With GUI War of Cards

Oct 27 2018 5:38 PM
 Why I get erorr on call show withe red 
namespace War1
{
public partial class War : Form
{
int SelectCard(out string suit)
{
int num = deck[lastCardIndex];
lastCardIndex++;
suit = suits[(num - 1) % 4];
return (num - 1) / 4 + 1;
}
int[] deck = new int[52]; // all zero by default
int lastCardIndex = 0;
string[] cards = { "Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King" };
string[] suits = { "clubs", "diamonds", "hearts", "spades" };
Random rand = new Random();
public War()
{
InitializeComponent();
}
private void Closebutton_Click(object sender, EventArgs e)
{
Close();
}
private void Button1_Click(object sender, EventArgs e)
{
pictureboxComputer.Image = Image.FromFile("../Pics/cardimages/2_of_clubs.png");
}
private void pictureBox1_Click(object sender, EventArgs e)
{
// pictureBox1.Image = Properties.Resources.myimage;
}
private void btnPlay_Click(object sender, EventArgs e)
{
string score;
string result;
int count = 26;
int totalComputer = 0;
int totalPlayer = 0;
int show = 0;
if ( count == 0)
{
if (totalComputer > totalPlayer)
score = "Computer wins!";
else if (totalComputer < totalPlayer)
score = "Player wins!";
else
score = "It's a draw!";
result = "";
result += String.Format("Final result\r\n\r\n");
result += String.Format(" Computer Score is {0}\r\n", totalComputer);
result += String.Format(" Player Score is {0}\r\n\r\n", totalPlayer);
result += score;
result += "\r\n\r\nPress Deal button to start another game";
lblResult.Text = result;
totalComputer = 0;
totalPlayer = 0;
btnDeal.Focus();
MessageBox.Show(result);
return;
}
string computerSuit;
int computer = show(out computerSuit) - 1;
string playerSuit;
int player = show(out playerSuit) - 1;
if (computer > player)
{
totalComputer += 2;
lblResult.Text = "Computer Wins";
score = "Computer gets 2 points";
}
else if (computer < player)
{
totalPlayer += 2;
lblResult.Text = "Player Wins";
score = "Player gets 2 points";
}
else
{
totalComputer += 1;
totalPlayer += 1;
score = "1 point each";
}
txtComputerPoints.Text = totalComputer.ToString();
txtPlayersPoints.Text = totalPlayer.ToString();
result = "";
result += String.Format("Show # {0}\r\n\r\n", show + 1);
result += String.Format(" Computer has {0} of {1}\r\n", cards[computer], computerSuit);
result += String.Format(" Player has {0} of {1}\r\n\r\n", cards[player], playerSuit);
result += score;
show++;
pictureboxComputer.Image = Image.FromFile(@"C:\temp\cardimages\" + cards[computer] + "_of_" + computerSuit + ".png");
pictureboxPlayer.Image = Image.FromFile(@"C:\temp\cardimages\" + cards[player] + "_of_" + playerSuit + ".png");
if (show == 26)
{
result += "\r\n\r\nGame over - The entire deck has been dealt!";
}
}
private void pictureBox2_Click(object sender, EventArgs e)
{
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
private void textBox2_TextChanged(object sender, EventArgs e)
{
}
private void lblResult_TextChanged(object sender, EventArgs e)
{
}
private void txtComputerPoints_TextChanged(object sender, EventArgs e)
{
}