Jump to content

  • Log In with Google      Sign In   
  • Create Account

C# Go Fish game help


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
2 replies to this topic

#1 popcorn   Members   -  Reputation: 151

Like
0Likes
Like

Posted 24 July 2009 - 02:41 PM

Ok I have worked my way through the Head First C# book and had done a Go fish game as one of the exercises. This was a very basic game without using graphics. After I finished the book I decided to see if I could make one using graphics with cards that the player could click on etc. This is what I have at the moment. However I would like to remove the textbox altogether and just let the game run in a purely graphical sense. Anyone have any ideas for how this could best be achieved? This is probably going to be confusing... One of the problems I find is that because everything runs of the game class, the whole game is run by calling the game.PlayOneRound method. The problem with this is that all the players have effectively played after this method is called so it is difficult to pull out the required information for screen updates etc while keeping this structure intact. I'd appreciate it if someone could have a look at the code. Full source code here Oh, you should ignore all the commented out code as this was code for testing purposes or ideas that have already been tried but didn't work as expected. It's safe to remove all commented out code from form1. Finally the actual game is in the bin folder. Release and debug are the same. [Edited by - popcorn on July 24, 2009 8:58:45 PM]

Sponsor:

#2 popcorn   Members   -  Reputation: 151

Like
0Likes
Like

Posted 25 July 2009 - 03:09 PM

This is the class which I specifically want someone to have a look at, I would like to keep all the code within the PlayOneRound method but need to pull out information as each player completes their turn and display this. Is there any way to do this or will I need to rewrite the class?


using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;

namespace GoFish2
{
class Game
{
private List<Player> players;
private Dictionary<Card.Values, Player> books;
private Deck stock;
private TextBox textBoxOnForm;

Rectangle clientRectangle;
List<Rectangle> southRects;
List<Rectangle> eastRects;
List<Rectangle> westRects;

const int cardWidth = 71;
const int cardHeight = 96;
const int spacing = 30;
const int sideSpacing = 20;

Dictionary<string, Bitmap> bitmaps;
Bitmap sideBack;

public Game(string playerName, string[] opponentNames, TextBox textBoxOnForm, Rectangle clientRect)
{
Random random = new Random();
this.textBoxOnForm = textBoxOnForm;
players = new List<Player>();
players.Add(new Player(playerName, random, textBoxOnForm));

foreach (string player in opponentNames)
players.Add(new Player(player, random, textBoxOnForm));

books = new Dictionary<Card.Values, Player>();
stock = new Deck();
Deal();
players[0].SortHand();

////
southRects = new List<Rectangle>();
eastRects = new List<Rectangle>();
westRects = new List<Rectangle>();

clientRectangle = clientRect;

Deck newdeck = new Deck();
string[] strArray = newdeck.GetCardNames();

List<string> str = new List<string>();

for (int i = 0; i < strArray.Count(); i++)
{
string[] tokens = strArray[i].Split(' ');
string tempStr;

tempStr = tokens[0] + tokens[2] + ".bmp";

str.Add(tempStr);
}

bitmaps = new Dictionary<string, Bitmap>();

for (int i = 0; i < strArray.Count(); i++)
{
string path = @"sCards\";
path = path + str[i];
Bitmap bitmap = new Bitmap(path);

bitmaps.Add(strArray[i], bitmap);
}

sideBack = new Bitmap(@"sCards\RotateBackBlue.bmp");

createRectangles();
players[0].SortHand();

}

private void createRectangles()
{
Point south = new Point();
south.X = (clientRectangle.Width / 2) - ((((cardWidth / 2) * players[0].CardCount) + 35) / 2);
south.Y = clientRectangle.Height - cardHeight - 5;

Point east = new Point();
east.X = 5;
east.Y = (clientRectangle.Height / 2) - ((((cardWidth / 2) * players[1].CardCount) + 35) / 2);

Point west = new Point();
west.X = clientRectangle.Width - cardHeight - 5;
west.Y = (clientRectangle.Height / 2) - ((((cardWidth / 2) * players[2].CardCount) + 35) / 2); ;

Rectangle rect;

for (int i = 0; i < players[0].CardCount; i++)
{
rect = new Rectangle(south.X, south.Y, cardWidth, cardHeight);
southRects.Add(rect);
south.X += spacing;
}

for (int i = 0; i < players[1].CardCount; i++)
{
rect = new Rectangle(east.X, east.Y, cardHeight, cardWidth);
eastRects.Add(rect);
east.Y += sideSpacing;
}

for (int i = 0; i < players[2].CardCount; i++)
{
rect = new Rectangle(west.X, west.Y, cardHeight, cardWidth);
westRects.Add(rect);
west.Y += sideSpacing;
}
}

private void Deal()
{
stock.Shuffle();

for (int cards = 0; cards < 5; cards++)
{
for (int i = 0; i < players.Count; i++)
{
players[i].TakeCard(stock.Deal());
}
}

foreach (Player player in players)
PullOutBooks(player);
}

private int getPlayerCard(Point pt)
{
int selected = 0;

for (int i = southRects.Count; i > 0; i--)
{
if (southRects[i - 1].Contains(pt))
{
selected = i;
break;
}
}

return selected - 1;
}

private void MovePlayerRect(int selectedCard, int offset)
{
Rectangle rect = southRects[selectedCard];
rect.Offset(0, offset);
southRects[selectedCard] = rect;
}

private bool selected = false;
private int oldSelectedCard;

public bool IsPlayerTurn()
{
return selected;
}

public int SelectCard(Point pt)
{
int selectedPlayerCard = getPlayerCard(pt);

if (selected)
MovePlayerRect(oldSelectedCard, 10);

if (selectedPlayerCard != -1)
{
selected = true;
MovePlayerRect(selectedPlayerCard, -10);
oldSelectedCard = selectedPlayerCard;
}
else
selected = false;

return selectedPlayerCard;
}

private List<string> info;

public List<string> GetInfo()
{
return info;
}

public bool PlayOneRound(int selectedPlayerCard)
{
info = new List<string>();

Console.WriteLine("player cards before: " + players[0].CardCount);
for (int i = 0; i < players.Count; i++)
{
if (i == 0)
{
Console.WriteLine("you clicked " + players[0].Peek(selectedPlayerCard).Name);

players[0].AskForACard(players, 0, stock, players[0].Peek(selectedPlayerCard).Value);


for (int j = 0; j < players[0].CardCount; j++)
{
Console.WriteLine(players[0].Peek(j).Name);
}
Console.WriteLine("\n\n");
}
else
{
if(players[i].CardCount != 0)
players[i].AskForACard(players, i, stock);
}


if (PullOutBooks(players[i]))
{
textBoxOnForm.Text += players[i].Name + " drew a new hand\r\n";
int numberOfCards = 0;

if (stock.Count < 5)
numberOfCards = stock.Count;
else
numberOfCards = 5;

for (int j = 0; j < numberOfCards; j++)
{
Console.WriteLine("Inside PlayOneRound - about to deal");
players[i].TakeCard(stock.Deal());
}
}
}

players[0].SortHand();
Console.WriteLine("player cards after: " + players[0].CardCount);
selected = false;
southRects.Clear();
eastRects.Clear();
westRects.Clear();
createRectangles();

if (stock.Count == 0)
{
//textBoxOnForm.Text += "The stock is out of cards. Game over!\r\n";
MessageBox.Show("The stock is out of cards. Game over!\r\n");
return true;
}
else
return false;
}

public bool PullOutBooks(Player player)
{
List<Card.Values> pulledBooks = player.PullOutBooks();

for (int i = 0; i < pulledBooks.Count; i++)
{
books.Add(pulledBooks[i], player);
}

if (player.CardCount == 0)
return true;
else
return false;
}

public string DescribeBooks()
{
string str = string.Empty;

foreach (KeyValuePair<Card.Values, Player> idBook in books)
str += idBook.Value.Name + " has a book of " + Card.Plural(idBook.Key) + "\r\n";

return str;
}

public string GetWinnerName()
{
Dictionary<string, int> winners = new Dictionary<string,int>();

foreach(Card.Values value in books.Keys)
{
string name = books[value].Name;

if(winners.ContainsKey(name))
winners[name]++;
else
winners.Add(name, 1);
}

int numberOfBooks = 0;

foreach (string player in winners.Keys)
{
if (winners[player] > numberOfBooks)
numberOfBooks = winners[player];
}

int totalBooks = 0;

foreach (string player in winners.Keys)
{
if (winners[player] == numberOfBooks)
totalBooks++;
}

string str = string.Empty;

foreach (string player in winners.Keys)
{
if (winners[player] == numberOfBooks)
{
if (!string.IsNullOrEmpty(str))
{
str += " and ";
}
str += player;
}
}

str += " with " + numberOfBooks + " books.";

if (totalBooks > 1)
return "A tie between " + str;
else
return str;
}

public string[] GetPlayerCardNames(int player)
{
return players[player].GetCardNames();
}

public string DescribePlayerHands()
{
string description = "";

for (int i = 0; i < players.Count; i++)
{
description += players[i].Name + " has " + players[i].CardCount;

if (players[i].CardCount == 1)
description += " card.\r\n";
else
description += " cards.\r\n";
}

description += "The stock has " + stock.Count + " cards left.\r\n";
return description;
}

public void Draw(Graphics g)
{
string[] playerCards = players[0].GetCardNames();

for (int i = 0; i < southRects.Count(); i++)
{
Point south = new Point();
south.X = southRects[i].Left;
south.Y = southRects[i].Top;
g.DrawImage(bitmaps[playerCards[i]], south.X, south.Y, 71, 96);
}

for (int i = 0; i < eastRects.Count; i++)
{
Point east = new Point();
east.X = eastRects[i].Left;
east.Y = eastRects[i].Top;
g.DrawImage(sideBack, east.X, east.Y, 96, 71);
}

for (int i = 0; i < westRects.Count; i++)
{
Point west = new Point();
west.X = westRects[i].Left;
west.Y = westRects[i].Top;
g.DrawImage(sideBack, west.X, west.Y, 96, 71);
}
}
}
}



Below is the code for the form with all the test code removed:


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

//The card size is 71 * 96

namespace GoFish2
{
public partial class Form1 : Form
{

Game game;

public Form1()
{
InitializeComponent();
game = new Game("Player 1", new string[] { "Joe", "Bob" }, textProgress, this.ClientRectangle);
button1.Enabled = false;
}

private void Form1_MouseClick(object sender, MouseEventArgs e)
{
Point pt = e.Location;

selectedCard = game.SelectCard(pt);
if (game.IsPlayerTurn())
button1.Enabled = true;
this.Invalidate();
}

int selectedCard;

private void Form1_Paint(object sender, PaintEventArgs e)
{
game.Draw(e.Graphics);
}

private void button1_Click(object sender, EventArgs e)
{
if(selectedCard != -1)
game.PlayOneRound(selectedCard);
if (!game.IsPlayerTurn())
button1.Enabled = false;
textProgress.SelectionStart = textProgress.Text.Length;
textProgress.ScrollToCaret();
this.Invalidate();
}
}
}




#3 popcorn   Members   -  Reputation: 151

Like
0Likes
Like

Posted 29 July 2009 - 05:53 PM

I managed to solve the problem I had but I would like some feedback on the game and the way it plays. I'd appreciate any suggestions concerning interface, does it play right, any bugs etc

Download the game here.

Also is this the right forum for this post. I see there is a .Net forum but I guess it could also go into the game programming forum as well. If it isn't then could someone move it to the appropriate forum.




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS