Game I am about to make:
Coins are randomly drawn from a set of possible coins and arranged in a line. Two players alternately remove and collect a coin from either end of the line. The game ends when all the coins are removed and collected by the two players. The winner is the one who has collected the larger total value. Assume that the number of coins is even and the total value of the coins is odd.
1.The program needs to be designed with a graphical user interface (GUI). There are two playing modes. The first is that both players are human, and the second is that one of them is a computer.
2. The code governing the rules of the game should be implemented with a separate static library.
3. Before a human user starts to play, he needs to login the system first. A file is then created to store his profile if this user is a new player. If this user has played this game before, his profile should be retrieved and shown on the screen. After he finishes the game, his profile should then be modified.

Based on my past experience I make a class for the player's profile to store their ID, times of win and times of lose and I intend to start the game by asking the player to log in. Then if I choose the human to human mode, I do not know how to store the second person's profile. Class codes for "Profile" is followed.

Another question is that how to show all the coins in a line waiting to be chosen from either side? Basically I just don' have any idea how to write the game rule in a class. To make it simpler at first, I just want to display say, 10 numbers in the GUI and choose from either end. My idea is to creat two buttons, one for choosing the left end, the other for the right. But how to realize that? Meanwhile how to keep two records for the two parties' numbers?

If one chooses to play the human to computer mode, how to let the computer do the selection automatically after the person does?

Thank you so much for advices and suggestions!

Codes for the profile class:

// ProfileLib.h
#include <fstream>
#include <iostream>
using namespace std;

class Profile
{
private:
    int id; 
    int win; 
    int lose; 

public:
    Profile(); 
    Profile(int id, int win, int lose); 
    ~Profile(); 
    int  getId();   
    int getWin(); 
    int  getLose();     
    void setId(int Id); 
    void setWin (int Win); 
	void setLose (int Lose);
    int read_Record(char *FName, int RecNo);
	int update_Record(char *FName);
};

The following is the implementation of the above class:

#include "ProfileLib.h"

Profile::Profile()
{
    id=0; 
    win=0; 
    lose=0; 
}// initialise the object with NULL strings and 0-valued integers,
             // overloading constructor 1
Profile::Profile(int ID, int Win, int Lose)
            // initialise according to the given parameters, overloading
            // constructor 2
{
	id=ID;
                win=Win;
	lose=Lose;
}
Profile::~Profile(){}; // destructor
int Profile::getId(){return id;} 
int Profile::getWin(){return win;}
int Profile::getLose(){return lose;}
void Profile::setId(int Id){id=Id;} 
void Profile::setWin (int Win){win=Win;}
void Profile::setLose (int Lose){lose=Lose;}

int Profile::read_Record(char *FName, int RecNo)
    // Read in record information from a file of name $FName[] according to the
    // record number $RecNo, and store them into the GAME object抯 member 
    // variables correspondingly.
    // If reading is successful, return 1; Otherwise, for whatever reason, 
    // return 0.
{
	char rec[256];
	ifstream rec_file(FName);
	while (rec_file.getline(rec,256))
	{
		if(RecNo==atoi(rec))
		{
			rec_file.getline(rec,256);
			setId(atoi(rec));
			rec_file.getline(rec,256);
			setWin(atoi(rec));
	        rec_file.getline(rec,256);
			setLose(atoi(rec));
			return 1;
		}
	}
	rec_file.close();
	return 0;
}

int Profile::update_Record(char *FName)
    // This member function updates the record of an existing game record in the 
    // file named $FName.
    // The record is uniquely identified by its timestamp (i.e., the timestamp
    // remains unchanged).
    // If the record is found and updated, the member function returns 1.
    // If the record is not found, add the record information at the end of the
    // file and the member function returns 2.
    // If the file reading is not successful, the member function returns 0.
{
	ifstream rec_file(FName);
	if (!rec_file)
		return 0;    // File reading not successful
	int count=0;    // Count the number of blank line
	int position=0; // Cursor position
	char rec[256];
	while (rec_file.getline(rec,256))
	{
		int s=atoi(rec);
		if(s-id==0)
			position=rec_file.tellg();
		if(strcmp(rec, "")==0)
			count++;
	}
	rec_file.close();
	if (position != 0)  // Profile found
	{
		ofstream wrec_file(FName, ios::in);
		wrec_file.seekp(position);
		wrec_file<<win<<'\n'<<lose;
		wrec_file.close();
		return 1;
	}
	else // Profile not found, add entry at the end
	{
		ofstream wrec_file(FName, ios::app); //create new record
		wrec_file<<"\n\n"<<count+2<<"\n"<<id<<"\n"<<win<<"\n"<<lose;
		wrec_file.close();
		return 2;
	}
	return 0; // For whatever reason, reading not successful
}

I have built it and succeeded.

Thanks in advance for your help!

Recommended Answers

All 2 Replies

I have come up with a method to display 10 picture boxes in GUI for the coin pictures. After I click one of them, what is the command to remove the picture box so it cannot be selected again?

What are you using for the gui program? OpenGL, DirectX, Direct Draw, QT, or something else? This is very very complex programming, so I hope you have a sound foundation in C and/or C++ programming because you will need it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.