示例#1
0
int main() {
	char playAgain;

	do {
		nodes_expanded = 0;
		//initialize the board.
		char *board = newBoard();

		//get player and computer characters.
		char playerPiece = askUser("\nPlease choose your character (x or o): ", X, O);
		char computerPiece = getOpponentPiece(playerPiece);

		//check if player wants to play first.
		char ch = askUser("\nDo you want to play first ? (y or n): ", 'y', 'n');
		if(ch == 'y') {
			makePlayerMove(board, playerPiece);			
		}
		else if(ch == 'n') {
		}

		char winner;
		int turn = COMPUTER;

		//game loop.
		while(!isFilled(board) && (winner = whoWon(board)) == NONE) {

			if(turn == COMPUTER) {
				makeComputerMove(board, computerPiece);
				turn = PLAYER;
			}
			else {
				makePlayerMove(board, playerPiece);
				turn = COMPUTER;
			}
		}

		printBoard(board);

		//print results.
		if(winner == playerPiece) {
			printf("\n$ Congratulations! you have won. $\n");
		}
		else if(winner == NONE) {
			printf("\n* The game is a draw. *\n");
		}
		else {
			printf("\n@ You Lose!! @\n");
		}

		printf("\nNumber of nodes expanded: %d", nodes_expanded);

		//ask if user wants to play again.
		playAgain = askUser("\n Do you want to play again? (y or n): ", 'y', 'n');
	}while(playAgain == 'y');

	return 0;
}
示例#2
0
void gameloop(struct game *state)
{

  printf("gameloop: current board '%s' with player %c\n",state->board,state->player);

  displayGame(state);

  if(gameover(state) == 1) {
	return;
  }

  makePlayerMove(state,getPlayerMove(state));

  displayGame(state);

  if(gameover(state) == 1) {
	return;
  }

  makeComputerMove(state);

  gameloop(state);

}