Example #1
0
State onAIsTurn(Game* game) {
  /* added clear screen -Cesar 1/27 */
  char cmd[255];
  int result;
  clearScreen();
  assert(game);
  printBoard(game);
  printf("Turn number: %d, ", game->turn+1);
  if(game->turn%2 == 0)
	  printf("WHITE's turn\n");
  else
	  printf("BLACK's turn\n");
  
  if(check500Turns(game))
	  return MainMenu;


  getDecisionFromAI(game, cmd);
  /* Normally this state should return to player's turn.
   * If AI vs. AI is need, then we should modify here.
   */
  result = gameLogic(game, cmd);
  if(result == 9 || result == 10)
	  result = promotion(game, cmd, 'Q');
  if(result == 7)
  {
	  printf("CHECKMATE\n");
	  saveLog(game, "lastGameLog.log");
	  printBoard(game);
	  getchar();
	  return MainMenu;
  }
  else if(result == 8)
  {
	  printf("STALEMATE\n");
	  saveLog(game, "lastGameLog.log");
	  printBoard(game);
	  getchar();
	  return MainMenu;
  }
  

  /*
  if (result == 7 || game->turn > 100)
    return TurnPlayer;
  else
    return TurnAI;
  */

  if(game->setting->gameMode == 2)
    return TurnAI;
  else
    return TurnPlayer;


}
Example #2
0
State onPlayersTurn(Game* game) {
  /* added clear screen -Cesar 1/27 */
  char cmd[255];
  char choice = 0;
  char userInput[255];
  char promotionChoice;
  char maxTurnUserInput;
  int result;
  int ox, oy, dx, dy;
  clearScreen();
  assert(game);
  printBoard(game);
  printf("Turn number: %d, ", game->turn+1);
  if(game->turn%2 == 0)
	  printf("WHITE's turn\n");
  else
	  printf("BLACK's turn\n");
  printUserMenu(game);
  getGameInput(cmd);
  choice = cmd[0];
  
  if(check500Turns(game))
	  return MainMenu;


  /* added by Cesar 1/26 */
  switch(choice)
    {
    case '0':
      return MainMenu;
    case '1':
      printHints(game);
      return TurnPlayer;
    case '2':
      redo(game);
      return TurnPlayer;
	case '3':
	  printLog(game);
	  return TurnPlayer;
    case '4':
      replay(game);
      return TurnPlayer;
    case '5':
	  printf("Enter the file name you want to save \n");
	  scanf("%s", userInput);
      saveLog(game, userInput);
      fseek(stdin,0,SEEK_END);
	  return TurnPlayer;
	case '6':
	  printf("Enter the file name you want to save: ");
	  scanf("%s", userInput);
	  printf("\n");
      fseek(stdin,0,SEEK_END);
	  saveGame(game, userInput);
	  return TurnPlayer;
	case '7':
		gameOptions(game);
		return TurnPlayer;
    default:
      break;
    }
  
  
	ox = cmd[0] - 97;
	oy = cmd[1] - 49;
	dx = cmd[2] - 97;
	dy = cmd[3] - 49;
	if((!checkBoarder(ox, oy))||(!checkBoarder(dx, dy)) || (isValidMove(game, ox, oy, dx, dy) <= 0))
	{
		printf("INVALID MOVE DETECTED");
		if(game->setting->allowIllegalMove == 1)
		{
			printf(", TRY AGAIN\n\n");
			return TurnPlayer;
		}
		else
		{
			printf(", GAME OVER!\n\n");
			return MainMenu;
		}
	}

  
  result = gameLogic(game, cmd);
  if(result == 9 || result == 10)
  {
	  promotionChoice = ' ';
	  while(promotionChoice != 'Q' && promotionChoice != 'R' && promotionChoice != 'N' && promotionChoice != 'B')
	  {
		  printf("Pawn promotion, enter the promotion choice (char to represent piece): ");
		  scanf("%c", &promotionChoice);
		  fseek(stdin,0,SEEK_END);
		  if(promotionChoice=='q') promotionChoice='Q';
		  else if(promotionChoice=='r') promotionChoice='R';
		  else if(promotionChoice=='n') promotionChoice='N';
		  else if(promotionChoice=='b') promotionChoice='B';
		  printf("%c\n",promotionChoice);
		  if(promotionChoice != 'Q' && promotionChoice != 'R' && promotionChoice != 'N' && promotionChoice != 'B')
			  printf("Invalid input, try again.\n");
	  }
      result = promotion(game, cmd, promotionChoice);
  }
  if(result == 7)
  {
	  printf("CHECKMATE\n");
	  getchar();
	  return MainMenu;
  }
  else if(result == 8)
  {
	  printf("STALEMATE\n");
	  getchar();
	  return MainMenu;
  }

  if(game->setting->gameMode == 0)
    return TurnAI;
  else if(game->setting->gameMode == 1)
    return TurnPlayer;
  else
    return MainMenu; /* game logic returns 0 meaning the game is end */
}
Example #3
0
int main(int argc, char *argv[]){
	int nbByteCom;
	int i=0, x, y, a, b, k, l, erreur, mauvMouv;
	char str2[100];
	char stop;
	int nbPara;
	int nbPionJ;
	int sockfd;
	DATA *dataBuf=(DATA*)malloc(sizeof(DATA));
	DAMIER *damier;
	PION *p;
	DATA dataFin;
	dataFin.type=FIN;
	damier = init_damier();
	if(damier==NULL){
		printf("Initialisation du damier échouée\n");
		return 0;
	}

	if(argc==2){
		printf("Server, en attente d'un client sur le port %s\n", argv[1]);
		sockfd=server(atoi(argv[1]));
		if(sockfd<0) return 1;
		aff_damier(*damier);

	}else if(argc==3){
		printf("Client, connection au server %s, sur le port %s\n", argv[1], argv[2]);
		sockfd=client(argv[1],atoi(argv[2]));
		if(sockfd<0) return 1;
		
		aff_damier(*damier);
		affTour(damier);
		printf("Nombre de prise possible : %d\n", verif_possibilite_prendre(damier));
		puts("En attente du joueur adverse...");
		nbByteCom=read(sockfd,dataBuf,sizeof(DATA));
		if(nbByteCom>0){
			while(dataBuf->type!=FIN){
				if(readData(damier,*dataBuf)){
					printf("Desynchronisation\n");
					close(sockfd);
					free(dataBuf);
					free_damier(damier);
					return 1;
				}
				aff_damier(*damier);
				nbByteCom=read(sockfd,dataBuf,sizeof(DATA));
			}
		}else{
			printf("Connection perdue\n");
			close(sockfd);
			if(dataBuf!=NULL) free(dataBuf);
			if(damier!=NULL) free_damier(damier);
			exit(0);
		}
		tour_suivant(damier);
	}else{
		printf("Nombre d'arguments incorrect\n");
		return 1;
	}


	while(!fin_partie(damier)){
		mauvMouv=1;
		promotion(damier);
		affTour(damier);
		printf("Nombre de prise possible : %d\n", verif_possibilite_prendre(damier));
		while(mauvMouv){
			mauvMouv=0;
			printf("Action sur un pion : ");
			nbPara=sscanf(fgets(str2,14,stdin),"%d,%d %d,%d %d,%d", &x, &y, &a, &b, &k, &l);
			if(nbPara<1){
				printf("Bye!\n");
				close(sockfd);
				if(dataBuf!=NULL) free(dataBuf);
				if(damier!=NULL) free_damier(damier);
				exit(0);	
			}
			while(nbPara<4){
				printf("paramètre manquant(%d)\n",nbPara);
				printf("Action sur un pion : ");
				nbPara=sscanf(fgets(str2,14,stdin),"%d,%d %d,%d %d,%d", &x, &y, &a, &b, &k, &l);
				if(nbPara<1){
					printf("Bye!\n");
					close(sockfd);
					if(dataBuf!=NULL) free(dataBuf);
					if(damier!=NULL) free_damier(damier);
					exit(0);	
				}
			}
			PION *p1 = rech_pion_c(damier,x,y);
			if(nbPara<6){
				k=a+(a-x);
				l=b+(b-y);
			}
			if(verif_possibilite_prendre(damier)==0){
				if(deplacer(damier,p1,a,b)==0){
					printf("Pion déplacé en (%d,%d)\n",a,b);
					nbByteCom=write(sockfd,&last_modif,sizeof(DATA));
				}else{
					printf("MAUVAIS MOUVEMENT\n");
					mauvMouv=1;
				}
			}else{
				if(prendre(damier,p1,rech_pion_c(damier,a,b),k,l)==0){
					printf("Prise de (%d,%d) par (%d,%d) en (%d,%d)\n",a,b,x,y,k,l);
					nbByteCom=write(sockfd,&last_modif,sizeof(DATA));
					while(verif_possibilite_prendre(damier)>0){
						aff_damier(*damier);
						printf("Rafle : ");
						nbPara=sscanf(fgets(str2,14,stdin),"%d,%d %d,%d %d,%d", &x, &y, &a, &b, &k, &l);
						if(nbPara<1){
							printf("Bye!\n");
							close(sockfd);
							if(dataBuf!=NULL) free(dataBuf);
							if(damier!=NULL) free_damier(damier);
							exit(0);	
						}
						while(nbPara<4){
							printf("paramètre manquant(%d)\n",nbPara);
							printf("Rafle : ");
							nbPara=sscanf(fgets(str2,14,stdin),"%d,%d %d,%d %d,%d", &x, &y, &a, &b, &k, &l);
							if(nbPara<1){
								printf("Bye!\n");
								close(sockfd);
								if(dataBuf!=NULL) free(dataBuf);
								if(damier!=NULL) free_damier(damier);
								exit(0);	
							}
						}
						if(nbPara<6){
							k=a+(a-x);
							l=b+(b-y);
						}
						if(prendre(damier,p1,rech_pion_c(damier,a,b),k,l)==0){
							printf("Prise de (%d,%d) par (%d,%d) en (%d,%d)\n",a,b,x,y,k,l);
							nbByteCom=write(sockfd,&last_modif,sizeof(DATA));
						}else{
							printf("Prise de (%d,%d) par (%d,%d) en (%d,%d)\n",a,b,x,y,k,l);
							printf("MAUVAIS MOUVEMENT\n");
						}
					}
				}else{
					printf("MAUVAIS MOUVEMENT\n");
					mauvMouv=1;
				}
			}
		}
		aff_damier(*damier);
		nbByteCom=write(sockfd,&dataFin,sizeof(DATA));
		tour_suivant(damier);
		affTour(damier);
		printf("Nombre de prise possible : %d\n", verif_possibilite_prendre(damier));
		printf("En attente du joueur adverse...\n");
		nbByteCom=read(sockfd,dataBuf,sizeof(DATA));
		if(nbByteCom>0){
			while(dataBuf->type!=FIN){
				if(readData(damier,*dataBuf)){
					printf("Desynchronisation\n");
					close(sockfd);
					free(dataBuf);
					free_damier(damier);
					return 1;
				}
				aff_damier(*damier);
				nbByteCom=read(sockfd,dataBuf,sizeof(DATA));
			}
		}else{
			printf("Connection perdue\n");
			close(sockfd);
			if(dataBuf!=NULL) free(dataBuf);
			if(damier!=NULL) free_damier(damier);
			exit(0);
		}
		tour_suivant(damier);
	}
	free_damier(damier);
	free(dataBuf);
	close(sockfd);
	return 0;
}
Example #4
0
inline void AI::movementsWithBeatingsFromTo(int i, int j, int x, int y, int step, vector <Coord> tempCoord) {
	if (step == 0) {

		// Tworzenie nowego wêz³a
		vector<Coord> temp2;
		temp2.push_back(Coord(i, j, x, y));
		_coord.push_back(temp2);

		//Przemieszczanie pionka
		Board::board[x][y] = Board::board[i][j];
		FIELD temp = Board::board[(i + x) / 2][(j + y) / 2];
		Board::board[(i + x) / 2][(j + y) / 2] = EMPTY;
		Board::board[i][j] = EMPTY;

		if ((y == 7 && board[x][y] == P_WHITE) || (y == 0 && board[x][y] == P_BLACK)) {

			promotion(x, y);
			if (Board::checkBeating(x, y)) {
				tempCoord = _coord.back();
				_coord.pop_back();
				movementWithBeatings(x, y, step + 1, tempCoord);
			}
			unPromotion(x, y);
		}
		else {
			// Rekurencyjnie sprawdzanie czy posiada jeszcze jakieœ bicia.
			movementWithBeatings(x, y, step + 1, tempCoord);
		}

		//Powrót pionka na w³aœciwe miejsce
		Board::board[i][j] = Board::board[x][y];
		Board::board[x][y] = EMPTY;
		Board::board[(i + x) / 2][(j + y) / 2] = temp;

	}
	else {
		if (!_coord.empty()) {
			_coord[_coord.size() - 1].push_back(Coord(i, j, x, y));
		}
		else {
			// Tworzenie nowego wêz³a
			vector<Coord> temp2;
			temp2.push_back(Coord(i, j, x, y));
			_coord.push_back(temp2);
		}

		//Przemieszczanie pionka
		Board::board[x][y] = Board::board[i][j];
		FIELD temp = Board::board[(i + x) / 2][(j + y) / 2];
		Board::board[(i + x) / 2][(j + y) / 2] = EMPTY;
		Board::board[i][j] = EMPTY;

		if ((y == 7 && board[x][y] == P_WHITE) || (y == 0 && board[x][y] == P_BLACK)) {

			promotion(x, y);
			if (Board::checkBeating(x, y)) {
				tempCoord = _coord.back();
				_coord.pop_back();
				movementWithBeatings(x, y, step + 1, tempCoord);
			}
			unPromotion(x, y);
		}
		else {
			// Rekurencyjnie sprawdzanie czy posiada jeszcze jakieœ bicia.
			movementWithBeatings(x, y, step + 1, tempCoord);
		}

		//Powrót pionka na w³aœciwe miejsce
		Board::board[i][j] = Board::board[x][y];
		Board::board[x][y] = EMPTY;
		Board::board[(i + x) / 2][(j + y) / 2] = temp;
	}
}