[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[pbmserv-dev] CooperYoung v1.0 programmed
Dear all,
I have (see below for programs) ported my game "Meet me at
Cooper and Young" and the London, Sydney, Chicago and Vegas variants for
the gamerz pbem server. What should I do with it to get it onto
Richard's computer? If you have any advice on how to improve the code,
please advise.
Luke
---------------------------------------------------
help cooperyoung
Meet me at Cooper and Young
(c) Luke Pebody 2003
CONCEPT
===*===
Two players secretly choose a row and column which combine to give a
meeting point. The players then take turns placing tiles on the board.
Whoever places their tile on the meeting point wins.
CHALLENGE
====*====
The challenge command is as follows
cooperyoung challenge <player1> <player2>
(options: -london -chicago -sydney -vegas)
Variants:
London - default
Each player places their own tiles only.
Sydney
Each player places their opponents tiles only.
Chicago
Each player chooses which tile to play.
Vegas
Before the player chooses where to play, a tile is randomly selected
for him.
MOVE NOTATION
======*======
The move command is as follows
cooperyoung move <player> <password> <move>
where <move> is
(i)
on player 1's first move the chosen column in the format D
(ii)
on player 2's second move the chosen row in the format 5
(iii)
thereafter a square in the format C7 (with the letter x or o after it
if playing Chicago variant)
WIN CONDITION
======x======
The players first moves together choose a square, the target square. A
player wins if his/her tile is placed (by either player) on the target
square.
-----------------------------
cooperyoung_main.cpp
#include "cooperyoung.h"
int main(int argc, char **argv)
{
CooperYoung cooperyoung;
cooperyoung.Main(argc,argv);
return 0;
}
-----------------------------
cooperyoung.h
#ifndef COOPERYOUNG_H
#define COOPERYOUNG_H
#include "board2d.h"
#include "lists.h"
class Chosen:public ReadWriteIntList {
protected:
const char *Name(void) { return "chosen"; };
};
class CooperYoung: public Board2D {
protected:
Chosen chosen;
static const char *Cols;
static const char Poss;
static const char *Pieces;
public:
virtual char Blank(void) { return '.'; }
virtual void PrintBoard(FILE *fp);
virtual void PrintBoardAs(FILE *fp, int what, int which);
virtual const char *MakeMove(const char *move);
virtual int MustSkip(void);
virtual const char *ForcedMove(void);
virtual int IsGameOver(const char *&winner);
virtual int WriteBoard(GameFile &game);
virtual int ReadBoard(GameFile &game);
// Non-board specific
virtual const char *GameType(void) { return "CooperYoung"; }
virtual int MoveWidth(void) { return 5; }
virtual int Init(void);
};
#endif
----------------------------------------
cooperyoung.cpp
#include <sys/types.h>
#include <ctype.h>
#include <time.h>
#include <stdlib.h>
#include <string.h>
#include <iostream.h>
#include "cooperyoung.h"
const char *CooperYoung::Cols="abcdefgh";
const char CooperYoung::Poss=63;
const char *CooperYoung::Pieces="ox";
void CooperYoung::PrintBoard(FILE *fp)
{
int i,j;
int r,c;
c=-1;
r=-1;
for (i=2 ; i<moves.Count() ; i++)
if (strcmp(moves[i],"Resign") &&
strcmp(moves[i],"Forfeit") && strcmp(moves[i],SkipMove())) {
c =
strchr(Cols,tolower(moves[i][strlen(moves[i])-2]))-Cols+MinCol();
r =
atoi(moves[i]+strlen(moves[i])-1)-1+MinRow();
}
if (chosen[2] == 0) fprintf(fp,"\n London\n");
if (chosen[2] == 1) fprintf(fp,"\n Sydney\n");
if (chosen[2] == 2) fprintf(fp,"\n Chicago\n");
if (chosen[2] == 3) fprintf(fp,"\n Vegas\n");
fprintf(fp," ");
for (j=0 ; j<Width() ; j++)
fprintf(fp,"%c ",toupper(Cols[j]));
fprintf(fp,"\n\n");
int namewidth = MAX(strlen(players[0]),strlen(players[1]));
for (i = MinRow() ; i <= MaxRow() ; i++) {
int n = i-MinRow()+1;
fprintf(fp," %d ",n);
for (j = MinCol(); j <= MaxCol(); j++)
if (r==i && c==j)
fprintf(fp, "%c ",toupper(GetAt(i,j)));
else
fprintf(fp, "%c ",GetAt(i,j));
fprintf(fp," %d",n);
fprintf(fp,"\n");
}
fprintf(fp,"\n ");
for (j=0 ; j<Width() ; j++)
fprintf(fp,"%c ",toupper(Cols[j]));
fprintf(fp,"\n");
}
void CooperYoung::PrintBoardAs(FILE *fp,int what,int who)
{
int i,j;
int r,c;
c=-1;
r=-1;
for (i=2 ; i<moves.Count() ; i++)
if (strcmp(moves[i],"Resign") &&
strcmp(moves[i],"Forfeit") && strcmp(moves[i],SkipMove())) {
c =
strchr(Cols,tolower(moves[i][strlen(moves[i])-2]))-Cols+MinCol();
r =
atoi(moves[i]+strlen(moves[i])-1)-1+MinRow();
}
if (chosen[3] != 2){
fprintf(fp," The next piece to place is an
%c\n\n",PlayerPieces(chosen[3]));
}
fprintf(fp," ");
for (j=0 ; j<Width() ; j++)
fprintf(fp,"%c ",toupper(Cols[j]));
fprintf(fp,"\n\n");
int namewidth = MAX(strlen(players[0]),strlen(players[1]));
for (i = MinRow() ; i <= MaxRow() ; i++) {
int n = i-MinRow()+1;
fprintf(fp," %d ",n);
for (j = MinCol(); j <= MaxCol(); j++)
if (r==i && c==j)
fprintf(fp, "%c ",toupper(GetAt(i,j)));
else
fprintf(fp, "%c ",
(IsBlank(i,j) && what == 1) ? (((who !=
0 || j != chosen[1]) && (who != 1 || i != chosen[0])) ? GetAt(i,j) :
Poss) : GetAt(i,j));
fprintf(fp," %d",n);
fprintf(fp,"\n");
}
fprintf(fp,"\n ");
for (j=0 ; j<Width() ; j++)
fprintf(fp,"%c ",toupper(Cols[j]));
fprintf(fp,"\n");
if (chosen[2] == 0) fprintf(fp,"\n London\n");
if (chosen[2] == 1) fprintf(fp,"\n Sydney\n");
if (chosen[2] == 2) fprintf(fp,"\n Chicago\n");
if (chosen[2] == 3) fprintf(fp,"\n Vegas\n");
}
const char *CooperYoung::MakeMove(const char *move)
{
if (moves.Count() == 0){
int col;
col=strchr(Cols,tolower(move[0]))-Cols;
if (move[1])
return (char*)Error("Please enter column
(A-%c)",toupper(Cols[Width()-1]));
if (col < 0 || col >= Width())
return (char*)Error("Please enter column
(A-%c)",toupper(Cols[Width()-1]));
chosen[1] = col;
return "*";
}
if (moves.Count() == 1){
int row;
row=move[0]-49;
if (move[1])
return (char*)Error("Please enter row
(1-%d)",Height());
if (row < 0 || row >= Width())
return (char*)Error("Please enter row
(1-%d)",Height());
chosen[0] = row;
if (chosen[2] == 3) chosen[3] = 1;
return "*";
}
if (moves.Count() > 1 && chosen[2] != 2){
int row, col;
static char oldmove[10];
static char newmove[4];
unsigned char c;
int i;
for (i=0 ; (oldmove[i]=tolower(move[i])) ; i++);
col = strchr(Cols,oldmove[0])-Cols;
row = -1;
if (col < 0 || col >= Width()) {
if (sscanf(oldmove, "%d%c", &row, &c) == 2) {
row -= 1;
col = strchr(Cols,c)-Cols;
}
} else {
if (sscanf(oldmove, "%c%d", &c, &row) == 2) {
row -= 1;
col = strchr(Cols,c)-Cols;
}
}
if (row < 0 || col < 0)
return (char*)Error("Please enter move as column
(A-%c) and row (1-%d)",toupper(Cols[Width()-1]),Height());
sprintf(newmove, "%c%d", toupper(Cols[col]), 1+row);
row += MinRow();
col += MinCol();
char who = 64;
if (!IsBlank(row,col))
return (char*)Error("%c%d is not vacant.",
toupper(Cols[col-MinCol()]),row-MinRow()+1);
if (chosen[2] == 0)
who = PlayerPieces(CurrentPlayer());
if (chosen[2] == 1)
who = PlayerPieces(1-CurrentPlayer());
if (chosen[2] == 3){
who = PlayerPieces(chosen[3]);
sprintf(newmove, "%c:%c%d",
toupper(who),toupper(Cols[col-MinCol()]),row-MinRow()+1);
chosen[3] = rand() % 2;
}
PutAt(row,col,who);
return newmove;
}
if (moves.Count() > 1 && chosen[2] == 2){
int row, col,pie;
static char oldmove[10];
static char newmove[4];
unsigned char c;
int i;
for (i=0 ; (oldmove[i]=tolower(move[i])) ; i++);
col = strchr(Cols,oldmove[0])-Cols;
row = -1;
if (col < 0 || col >= Width()) {
if (sscanf(oldmove, "%d%c", &row, &c) == 2) {
row -= 1;
col = strchr(Cols,c)-Cols;
}
} else {
if (sscanf(oldmove, "%c%d", &c, &row) == 2) {
row -= 1;
col = strchr(Cols,c)-Cols;
}
}
pie = strchr(Pieces,oldmove[2])-Pieces;
if (row < 0 || col < 0 || pie<0 || pie>1)
return (char*)Error("Please enter move as column
(A-%c), row (1-%d) and piece
(%c,%c)",toupper(Cols[Width()-1]),Height(),Pieces[0],Pieces[1]);
sprintf(newmove, "%c:%c%d", Pieces[pie],
toupper(Cols[col]), 1+row);
row += MinRow();
col += MinCol();
char who = Pieces[pie];
PutAt(row,col,who);
return newmove;
}
}
int CooperYoung::MustSkip(void)
{
return 0;
}
const char *CooperYoung::ForcedMove(void)
{
int r,c,n=0;
int sr,sc;
static char forced[3];
for (r=MinRow() ; r<=MaxRow() ; r++)
for (c=MinCol() ; c<=MaxCol() ; c++) {
if (!IsBlank(r,c))
continue;
if (n)
return NULL;
sr=r; sc=c;
n++;
}
sprintf(forced,"%c%d",toupper(Cols[sc-MinCol()]),sr-MinRow()+1);
return forced;
}
int CooperYoung::ReadBoard(GameFile &game)
{
int result = board.Read(game);
result |= chosen.Read(game);
return result;
}
int CooperYoung::WriteBoard(GameFile &game)
{
int result = board.Write(game);
result *= chosen.Write(game);
return result;
}
int CooperYoung::IsGameOver(const char *&winner)
{
if (chosen[0] == -1 || chosen[1] == -1)
return 0;
if (IsBlank(chosen[0],chosen[1]))
return 0;
if (GetAt(chosen[0],chosen[1]) == PlayerPieces(0))
winner = players[0];
else
winner = players[1];
return 1;
}
int CooperYoung::Init(void)
{
chosen.Init();
chosen.Add(-1);
chosen.Add(-1);
chosen.Add(0);
chosen.Add(2);
int i;
for (i=0;i<parameters.Count();i++)
{
if (!strcmp(parameters[i],"-london")) chosen[2]=0;
if (!strcmp(parameters[i],"-sydney")) chosen[2]=1;
if (!strcmp(parameters[i],"-chicago")) chosen[2]=2;
if (!strcmp(parameters[i],"-vegas")) chosen[2]=3;
}
board.Init();
char *row = new char[strlen(Cols)+1];
memset(row,Blank(),strlen(Cols));
row[strlen(Cols)] = '\0';
int r;
for (r = 0;r<strlen(Cols);r++)
board.Add(row);
delete [] row;
return 1;
}