[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[pbmserv-dev] Entirely new question from the new guy



Hey gang,

   Now that my server runs, I'm trying out coding a simple game just to see
if I can.  The code compiles, I can successfully initiate a challenge and
see the startup boards, but when the first player sends the first move, the
program returns no output.  If the second player tries to send a move, he
gets a message saying that it's not his turn, and shows the board.  Like so:

---------------------------
Instructor@Instructor /cygdrive/c/temp
$ ./tictactoe move 9 test1 test1 1

Instructor@Instructor /cygdrive/c/temp
$ ./tictactoe move 9 test2 test2 1
From: Richard's PBeM Server <pbmserv@gamerz.net>
Message-Id: <pbmserv.1120082477.3784.9@play.gamerz.net>
To: test1@abacustech.info
Subject: TicTacToe Board 9 Error

It is not your move

Summary of TicTacToe Board 9

Please start the game test1.

   Ohs (o)      Eks (x)
   test1        test2


 . | . | .
-------------
 . | . | .
-------------
 . | . | .
#include <sys/types.h>
#include <ctype.h>
#include <time.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>

#include "tictactoe.h"
 
const char *Tictactoe::Cols = "abc";

void Tictactoe::PrintBoard(FILE *fp)
{
	
	fprintf(fp, " %c | %c | %c\n",GetAt(0,0),GetAt(0,1),GetAt(1,2));
	fprintf(fp, "-------------\n");
	fprintf(fp, " %c | %c | %c\n",GetAt(1,0),GetAt(1,1),GetAt(1,2));
	fprintf(fp, "-------------\n");
	fprintf(fp, " %c | %c | %c\n",GetAt(2,0),GetAt(2,1),GetAt(2,2));
}

const char *Tictactoe::DecodeMove(const char *move, int &col, int &row)
{
	//static char * newmove;
	/* Move is an integer 1-9
	
	 1 | 2 | 3
	 ---------
	 4 | 5 | 6
	 ---------
	 7 | 8 | 9
	
	*/

	col = (atoi(move)-1) % 3;
	row = (int)floor(atoi(move)/3);

	if (col < 0 || col > 2 || row < 0 || row > 2) {
		return (char*)Error("Please enter a number between 1 and 9");
	}
	//sprintf(newmove, "%c,%c", row,col);

	return 0;
}

const char *Tictactoe::MakeMove(const char *move)
{
	int row, col;
	//const char *newmove = DecodeMove(move,col,row);

	//if (newmove == NULL) return NULL;

	col = (atoi(move)-1) % 3;
	row = (int)floor(atoi(move)/3);

	if (!IsBlank(row,col))
		return (char*)Error("Position %s is occupied.  Try another position", move);

	PutAt(col,row, PlayerPieces(CurrentPlayer()));

	last.Init();
	last.Add(row).Add(col);

	return 0;
}


int Tictactoe::IsGameOver(const char *&winner)
{
	int row = last[0];
	int col = last[1];
	/* Check for win... */

	if  ((GetAt(0,0)==GetAt(0,1)==GetAt(0,2)==PlayerPieces(CurrentPlayer())) ||
	     (GetAt(1,0)==GetAt(1,1)==GetAt(1,2)==PlayerPieces(CurrentPlayer())) ||
	     (GetAt(2,0)==GetAt(2,1)==GetAt(2,2)==PlayerPieces(CurrentPlayer())) ||
	     (GetAt(0,0)==GetAt(1,0)==GetAt(2,0)==PlayerPieces(CurrentPlayer())) ||
	     (GetAt(0,1)==GetAt(1,1)==GetAt(2,1)==PlayerPieces(CurrentPlayer())) ||
	     (GetAt(0,2)==GetAt(1,2)==GetAt(2,2)==PlayerPieces(CurrentPlayer())) ||
	     (GetAt(0,0)==GetAt(1,1)==GetAt(2,2)==PlayerPieces(CurrentPlayer())) ||
	     (GetAt(0,2)==GetAt(1,1)==GetAt(2,0)==PlayerPieces(CurrentPlayer())) ) { 
		 winner = players[CurrentPlayer(-1)];
		 return 1;
	}

	for (col=0 ; col<= 2 ; col++)
		for (row=0 ; row <=2 ; row++)
		if (IsBlank(col,row))
			return 0;

	winner = NULL;
	return 1;
}


int Tictactoe::Init(void)
{
	if (parameters.Count() > 0)
		return Error("%s does not take parameters", GameType());
	last.Init();
	last.Add(Unused).Add(Unused);
	return Board2D::Init(3,3);
}
#include "tictactoe.h"

int main(int argc, char **argv)
{
	Tictactoe tictactoe;

	tictactoe.Main(argc,argv);

	return 0;
}
#ifndef TICTACTOE_H
#define TICTACTOE_H

#include "board2d.h"
#include "lists.h"

class Last:public ReadWriteIntList {
protected:
	const char *Name(void) { return "last"; }
};

class Tictactoe: public Board2D {
protected:
	Last last;
	static const char *Cols;
public:
	virtual char Blank(void) { return '.'; }
	
	virtual void PrintBoard(FILE *fp);

	virtual const char *DecodeMove(const char *move, int &col, int&row);
	virtual const char *MakeMove(const char *move);
	//virtual const char *ForcedMove(void);
	//virtual int CountLine(int row, int col, int drow, int dcol);
	virtual int IsGameOver(const char *&winner);

// Non-board specific
	virtual const char *GameType(void) { return "TicTacToe"; }
	virtual int MoveWidth(void) { return 1; }

	virtual int Init(void);
};

#endif