Hello !
I have been trying to write a poker game program. i added 52 playing cards to an array. I can give the user 5 cards randomly. But the problem is that it should not be the same cards in a hand.i could not do that. Here are the codes :
string[] kagitlar ={"Kupa 1","Kupa 2","Kupa 3","Kupa 4","Kupa 5","Kupa 6","Kupa 7","Kupa 8",
"Kupa 9","Kupa 10","Kupa Kiz","Kupa Vale","Kupa Papaz",
"Sinek 1","Sinek 2","Sinek 3","Sinek 4","Sinek 5","Sinek 6","Sinek 7","Sinek 8",
"Sinek 9","Sinek 10","Sinek Vale","Sinek Kiz","Sinek Papaz",
"Karo 1","Karo 2","Karo 3","Karo 4","Karo 5","Karo 6","Karo 7","Karo 8","Karo 9",
"Karo 10","Karo Vale","Karo Kiz","Karo Papaz",
"Maça 1","Maça 2","Maça 3","Maça 4","Maça 5","Maça 6","Maça 7","Maça 8","Maça 9",
"Maça 10","Maça Vale","Maça Kiz","Maça Papaz"};
privatevoid button1_Click(object sender, EventArgs e)
{
Random rd = newRandom();
for (int i = 5; i>0; i--)
{
int indeks = rd.Next(0, kagitlar.Length);
listBox1.Items.Add(kagitlar[indeks].ToString());
//kagitlar[indeks].Remove(kagitlar[indeks]); i tried this,did not work...
}
}
thanks ..
yusufPosted Nov 23, 2007, 11:22 AM
The problem has been solved Alan. No more questions about Card game. I promise :)
AlanPosted Nov 23, 2007, 10:50 AM
Hi Yusuf.
When you tried my last lot of code did you place the following line before the 'p' for loop?
string[] temp = (string[])kagitlar.Clone();
If you did, then each player should be dealt a different set of cards from the one pack because, when you go around the loop for player 2, the 'temp' array will contain empty strings for the cards dealt to player 1 and so on for the other players.
yusufPosted Nov 23, 2007, 7:39 AM
It can give 5 cards to each listboxes Alan.Every listboxes dont have the same card.That is good. But for example player A(listbox1) could have the same card with player B(listbox2). I tried to prevent it with changing do-while place. I added it the top of codes.
It doesnt give the same cards now, but all listboxes dont fill with 5 cards either.. Everytime i push the button, sometimes it gives 4 cards sometimes 2 sometimes even 7...
Random rd = new Random();listBoxes =
new ListBox[4] { listBox1, listBox2, listBox3, listBox4 }; string[] temp = (string[])kagitlar.Clone(); int tane = 0; do // i replaced it..{
for (int p = 0; p < 4; p++) // 4 player oldugu icin for kullanildi...{
string[] dizi; int indeks = rd.Next(0, temp.Length); if (temp[indeks] != ""){
listBoxes[p].Items.Add(temp[indeks]);
temp[indeks] =
""; // kart secilmisse yeri degistirildi...tane++;
}
int count = 0; string[] dizi2; string deger = ""; for (int i = 0; i < listBoxes[p].Items.Count; i++){
for (int j = 0; j < listBoxes[p].Items.Count; j++){
if (i != j) // ayni indexteki elemani saymamak için kullanildi...{
dizi = (listBoxes[p].Items[i].ToString().Split(
' '));dizi2 = (listBoxes[p].Items[j].ToString().Split(
' ')); if (dizi[1] == dizi2[1]){
deger = dizi[1];
// elemani alir...count++;
}
}
}
}
if (count == 0){
MessageBox.Show("Elinizde Sirali yok!");}
if (count == 2){
MessageBox.Show("Elinizde 2 li var");}
if (count == 6){
MessageBox.Show("Elinizde 3 lü var");}
if (count == 12 && deger == "1" || count == 12 && deger == "2" || count == 12 && deger == "3"|| count == 12 && deger ==
"4" || count == 12 && deger == "5" || count == 12 && deger == "6"|| count == 12 && deger ==
"7" || count == 12 && deger == "8" || count == 12 && deger == "9"|| count == 12 && deger ==
"10" || count == 12 && deger == "Vale" || count == 12 && deger == "Kiz"|| count == 12 && deger ==
"Papaz"){
MessageBox.Show("Elinizde 4 lü " + deger + " var");}
else{
if (count != 20 && count != 0 && count != 2 && count != 6){
MessageBox.Show("Elinizde 2 tane 2 li var");}
}
if (count == 20 && deger == "1" || count == 20 && deger == "2" || count == 20 && deger == "3"|| count == 20 && deger ==
"4" || count == 20 && deger == "5" || count == 20 && deger == "6"|| count == 20 && deger ==
"7" || count == 20 && deger == "8" || count == 20 && deger == "9"|| count == 20 && deger ==
"10" || count == 20 && deger == "Vale" || count == 20 && deger == "Kiz"|| count == 20 && deger ==
"Papaz"){
MessageBox.Show("Elinizde 5 li " + deger + " var");}
if (count == 20){
MessageBox.Show("Elinizde bir üçlü, bir de ikili var");}
}
}
// end 'p' for loop while (tane <20);yusufPosted Nov 22, 2007, 5:54 PM
Alan, by helping me in that way, you can't imagine how you effect my improvement in c#. it has been just 3 days i am here, but the improvement is great ! .. it would not enough if i thank to you thousand of times to teach these informations..
AlanPosted Nov 22, 2007, 4:14 PM
As the cards need to be different, I think you'll have to deal all four hands from within the button1_Click eventhandler. I'd be inclined to put the 4 listboxes into an array field so that you can index them easily and then loop through the code four times. Something like this - just done 'off the top' so may not be quite right:
// new array field
private ListBox[] listBoxes;
private void button1_Click(object sender, EventArgs e)
{
Random rd = new Random();
string[] temp = (string[])kagitlar.Clone();
listBoxes = new ListBox[4]{listBox1, listBox2, listBox3, listBox4};
for (int p = 0 ; p < 4 ; p++) // p represents player number
{
string[] dizi;
dizi = listBoxes[p].Text.Split(' ');
int tane = 0;
do
{
int indeks = rd.Next(0, temp.Length);
if (temp[indeks] != "")
{
listBoxes[p].Items.Add(temp[indeks]);
temp[indeks] = ""; // kart secilmisse yeri degistirildi...
tane++;
}
}
while (tane < 5);
int count = 0;
string[] dizi2;
string deger = "";
for (int i = 0; i < listBoxes[p].Items.Count; i++)
{
dizi = (listBoxes[p].Items[i].ToString().Split(' '));
for (int j = 0; j < listBoxes[p].Items.Count; j++)
{
if (i != j) // ayni indexteki elemani saymamak için kullanildi...
{
dizi = (listBoxes[p].Items[i].ToString().Split(' '));
dizi2 = (listBoxes[p].Items[j].ToString().Split(' '));
if (dizi[1] == dizi2[1])
{
deger = dizi[1]; // elemani alir...
count++;
}
}
}
}
// ayni kartlardan kac tane 2'li, 3'lü, 4'lü ve 5'li olup olmadigi sorgulandi...
if (count == 0)
{
MessageBox.Show("Elinizde Sirali yok!"); // You have nothing
}
if (count == 2)
{
MessageBox.Show("Elinizde 2 li var"); // You have double
return;
}
if (count == 6)
{
MessageBox.Show("Elinizde 3 lü var"); // You have three
return;
}
if (count == 12 && deger == "1" || count == 12 && deger == "2" || count == 12 && deger == "3"
|| count == 12 && deger == "4" || count == 12 && deger == "5" || count == 12 && deger == "6"
|| count == 12 && deger == "7" || count == 12 && deger == "8" || count == 12 && deger == "9"
|| count == 12 && deger == "10" || count == 12 && deger == "Vale" || count == 12 && deger == "Kiz"
|| count == 12 && deger == "Papaz")
{
MessageBox.Show("Elinizde 4 lü " + deger + " var"); //You have four...
}
else
{
if (count != 20 && count!=0)
{
MessageBox.Show("Elinizde 2 tane 2 li var");// There are two doubles
return;
}
}
if (count == 20 && deger == "1" || count == 20 && deger == "2" || count == 20 && deger == "3"
|| count == 20 && deger == "4" || count == 20 && deger == "5" || count == 20 && deger == "6"
|| count == 20 && deger == "7" || count == 20 && deger == "8" || count == 20 && deger == "9"
|| count == 20 && deger == "10" || count == 20 && deger == "Vale" || count == 20 && deger == "Kiz"
|| count == 20 && deger == "Papaz")
{
MessageBox.Show("Elinizde 5 li " + deger + " var"); // You have five..
return;
}
if (count == 20)
{
MessageBox.Show("Elinizde bir üçlü, bir de ikili var"); //You've a double and three
return;
}
} // end 'p' for loop
}
private void button2_Click(object sender, EventArgs e)
{
for(int i = 0; i < listBoxes.Length; i++)
{
listBoxes[i].Items.Clear();
}
}
yusufPosted Nov 22, 2007, 3:33 PM
Program is working thanks a lot. The last thing i am going to ask you is, I will add 3 more listboxes because it must be 4 players.. I think that Method has to be used in that case.. The code blocks should work for 4 listboxes and the cards should not be the same too... I tried but i am not capable of doing this right now. Thanks again...
Here are the codes:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace PokerOyunu
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
string[] kagitlar ={"Kupa 1","Kupa 2","Kupa 3","Kupa 4","Kupa 5","Kupa 6","Kupa 7","Kupa 8",
"Kupa 9","Kupa 10","Kupa Kiz","Kupa Vale","Kupa Papaz",
"Sinek 1","Sinek 2","Sinek 3","Sinek 4","Sinek 5","Sinek 6","Sinek 7","Sinek 8",
"Sinek 9","Sinek 10","Sinek Vale","Sinek Kiz","Sinek Papaz",
"Karo 1","Karo 2","Karo 3","Karo 4","Karo 5","Karo 6","Karo 7","Karo 8","Karo 9",
"Karo 10","Karo Vale","Karo Kiz","Karo Papaz",
"Maça 1","Maça 2","Maça 3","Maça 4","Maça 5","Maça 6","Maça 7","Maça 8","Maça 9",
"Maça 10","Maça Vale","Maça Kiz","Maça Papaz"};
private void button1_Click(object sender, EventArgs e)
{
Random rd = new Random();
string[] dizi;
dizi = listBox1.Text.Split(' ');
string[] temp = (string[])kagitlar.Clone();
int tane = 0;
do
{
int indeks = rd.Next(0, temp.Length);
if (temp[indeks] != "")
{
listBox1.Items.Add(temp[indeks]);
temp[indeks] = ""; // kart secilmisse yeri degistirildi...
tane++;
}
}
while (tane < 5);
int count = 0;
string[] dizi2;
string deger = "";
for (int i = 0; i < listBox1.Items.Count; i++)
{
dizi = (listBox1.Items[i].ToString().Split(' '));
for (int j = 0; j < listBox1.Items.Count; j++)
{
if (i != j) // ayni indexteki elemani saymamak için kullanildi...
{
dizi = (listBox1.Items[i].ToString().Split(' '));
dizi2 = (listBox1.Items[j].ToString().Split(' '));
if (dizi[1] == dizi2[1])
{
deger = dizi[1]; // elemani alir...
count++;
}
}
}
}
// ayni kartlardan kac tane 2'li, 3'lü, 4'lü ve 5'li olup olmadigi sorgulandi...
if (count == 0)
{
MessageBox.Show("Elinizde Sirali yok!"); // You have nothing
}
if (count == 2)
{
MessageBox.Show("Elinizde 2 li var"); // You have double
return;
}
if (count == 6)
{
MessageBox.Show("Elinizde 3 lü var"); // You have three
return;
}
if (count == 12 && deger == "1" || count == 12 && deger == "2" || count == 12 && deger == "3"
|| count == 12 && deger == "4" || count == 12 && deger == "5" || count == 12 && deger == "6"
|| count == 12 && deger == "7" || count == 12 && deger == "8" || count == 12 && deger == "9"
|| count == 12 && deger == "10" || count == 12 && deger == "Vale" || count == 12 && deger == "Kiz"
|| count == 12 && deger == "Papaz")
{
MessageBox.Show("Elinizde 4 lü " + deger + " var"); //You have four...
}
else
{
if (count != 20 && count!=0)
{
MessageBox.Show("Elinizde 2 tane 2 li var");// There are two doubles
return;
}
}
if (count == 20 && deger == "1" || count == 20 && deger == "2" || count == 20 && deger == "3"
|| count == 20 && deger == "4" || count == 20 && deger == "5" || count == 20 && deger == "6"
|| count == 20 && deger == "7" || count == 20 && deger == "8" || count == 20 && deger == "9"
|| count == 20 && deger == "10" || count == 20 && deger == "Vale" || count == 20 && deger == "Kiz"
|| count == 20 && deger == "Papaz")
{
MessageBox.Show("Elinizde 5 li " + deger + " var"); // You have five..
return;
}
if (count == 20)
{
MessageBox.Show("Elinizde bir üçlü, bir de ikili var"); //You've a double and three
return;
}
}
private void button2_Click(object sender, EventArgs e)
{
listBox1.Items.Clear();
}
}
}
AlanPosted Nov 21, 2007, 1:02 PM
See if this is any better:
// you may have already declared these variables
string[] dizi = null;
string[] dizi2 = null;
int count = 0;
// but these are new
string three = "";
string four = "";
for (int i = 0; i < listBox1.Items.Count - 1; i++)
{
dizi = listBox1.Items[i].ToString().Split(' ');
count = 1;
for (int j = i + 1; j < listBox1.Items.Count; j++)
{
dizi2 = listBox1.Items[j].ToString().Split(' ');
if (dizi[1] == dizi2[1])
{
count++;
}
}
if (count == 2 && (dizi[1] != three) && (dizi[1] != four))
MessageBox.Show("You have two " + dizi[1] + "'s");
else if (count == 3)
{
MessageBox.Show("You have three " + dizi[1] + "'s");
three = dizi[1];
}
else if (count == 4)
{
MessageBox.Show("You have four " + dizi[1] + "'s");
four = dizi[1];
}
}
yusufPosted Nov 21, 2007, 12:11 PM
One more thing that i am in trouble. I have to report if the user’s hand has the double or the three . For example in the hand, there are Hearts 2,Spade 2 and Diamond 2. The report should be ‘You have three’ .
I used Split method to get the number of the card.
for (int i = 0; i
{
for (int j = 1; j < listBox1.Items.Count; j++)
{
dizi = (listBox1.Items[i].ToString().Split(' '));
dizi2 = (listBox1.Items[j].ToString().Split(' '));
if (dizi[1] == dizi2[1])
{
count++;
}
MessageBox.Show(dizi[1].ToString());
MessageBox.Show(dizi2[1].ToString());
}
}
i wrote these codes. It compares the members one by one. But the problem is it compares with itself too. For this reason count is increasing….
Thank you!
yusufPosted Nov 20, 2007, 7:53 PM
AlanPosted Nov 20, 2007, 7:30 PM
There are various ways of doing this but, as you've started with an array, I suggest you simply replace cards that have been drawn with an empty string. You can then keep on drawing cards randomly until you choose five that haven't been drawn already:
private void button1_Click(object sender, EventArgs e)
{
Random rd = new Random();
string[] temp = (string[])kagitlar.Clone(); // if using a fresh deck for each hand
int count = 0;
do
{
int indeks = rd.Next(0, temp.Length);
if (temp[indeks] != "")
{
listBox1.Items.Add(temp[indeks]);
temp[indeks] = ""; // once a card is drawn replace it with ""
count++;
}
}
while (count < 5);
}