Beispiel #1
0
void player_turn()
{
	int pos;

	check_draw();
	draw_board();
	gotoxy(30, 18);
	printf("Your Turn :> ");
	scanf("%d", &pos);

	if (board[convert_pos_to_row(pos)][convert_pos_to_col(pos)] != EMPTY)
		player_turn();

	move_to(pos);
	draw_board();

	if (isWinning(player))
	{
		gotoxy(30, 20);
		SetConsoleTextAttribute(h, FOREGROUND_RED);
		printf("Player Wins\n");
		updateNumberOfWins();
		SetConsoleTextAttribute(h, 7);
		_getch();
		menu();
		//exit(0);
	}
	else
		start_game();
}
Beispiel #2
0
void	on_client_hello(int packet_datas)
{
  GAME.game_started = 1;
  send_packet(S_HELLO, 0);
  my_putstr("enemy connected\n\n");
  player_turn();
}
Beispiel #3
0
void			start_game(t_grid *grid)
{
	int			random;
	int			i;
	int			winner;

	ft_putendl("starting the game");
	srand(time(NULL));
	random = rand() % 2;
	i = 0;
	while ((!(winner = check_winner(grid, 'X')))
			&& (!(winner = check_winner(grid, 'O'))) && (!(winner = tie(grid))))
	{
		random ^= 1;
		if (random)
		{
			while (player_turn(grid) == 0)
				i++;
		}
		else
			ai_turn(grid);
	}
	display_grid(grid);
	call_winner(winner);
}
void controller_mouseMotionFunc(int x, int y) {
    if (left_button_state == GLUT_DOWN) {
        std::pair<double,double> rotation = std::pair<double,double>(
            (x - mouse_pos.first) / (double) width,
            (y - mouse_pos.second) / (double) height
        );
        
        player_turn(rotation);
        
        if (x < 0) {
            x = width;
            glutWarpPointer(x, y);
        } else if (x > width) {
            x = 0;
            glutWarpPointer(x, y);
        }

        if (y < 0) {
            y = height;
            glutWarpPointer(x, y);
        } else if (y > height) {
            y = 0;
            glutWarpPointer(x, y);
        }
    }

    mouse_pos = std::pair<int, int>(x, y);
    glutPostRedisplay();
}
Beispiel #5
0
int main ( void ) {

//Declare variables
int board[BOARD_SIZE][BOARD_SIZE];
int turnnumber = 0;	 // Decide what turn number it is
int no_moves = 4;	// Number of pieces on the board

// Welcome message
printf("*******************************************************\n");
printf("* Welcome to Othello! The main objective of this game *\n");
printf("* is to outscore your oppponent and whoever has more  *\n");
printf("* pieces remaining wins the game! Please enter moves  *\n");
printf("* in the format of 'x y', without quotes. Have fun!   *\n");
printf("*                                                     *\n"); 
printf("*                                                     *\n");
printf("*             Coded by Iheanyi Ekechukwu              *\n");
printf("*******************************************************\n\n");

// Starting positions
default_board(board);

// Gameplay Loop
while(no_moves < BOARD_SIZE*BOARD_SIZE){

	print_board(board); // Continuously prints board
	if(turnnumber % 2 == 0) {
	
		player_turn(board, 1);
		no_moves++;
}
	else if(turnnumber % 2 == 1) {

		player_turn(board, -1);
		no_moves++;

}

turnnumber++;

}

print_board(board);
who_won(board);

return 0;
}
//Allow Dealer/Player to act on their turn
static void change_turn(int to)
{
	check_win();

	if(gameRunning == 1)
	{
		if(to == DEALER)
		{
			turn = DEALER;
			dealer_turn();
		}else{
			turn = PLAYER;
			player_turn();
		}
	}
}
Beispiel #7
0
void menu()
{
	int choice;
	playerMenu();
	printf("Press Enter to continue\n");
	_getch();

	system("cls");
	initialize_game_board();
	printf("\n--------TIC-TAC-TOE MENU--------");
	printf("\n1 : Play with X");
	printf("\n2 : Play with O");
	printf("\n3 : View all players");
	printf("\n4 : View top ten players");
	printf("\n0 : Exit");
	printf("\nEnter your choice:>");
	scanf_s("%d", &choice);
	turn = 1;
	switch (choice)
	{
	case 1:
		player = 1;
		comp = 0;
		player_turn();
		break;
	case 2:
		player = 0;
		comp = 1;
		start_game();
		break;
	case 3:
		viewAllPlayers();
		menu();
		break;
	case 4:
		viewTopTenPlayers();
		menu();
		break;
	case 0:
		printPlayerList();
		playerMenu();
		break;
	default:
		menu();
	}
}
Beispiel #8
0
int			launch_player(t_lemipc *lemipc)
{
  while (lemipc->is_dead == FALSE)
    if (player_turn(lemipc) == EXIT_FAILURE)
      return (EXIT_FAILURE);
  lemipc->map[lemipc->pos] = 0;
  if (lemipc->is_alone == TRUE && player_is_alone(lemipc, TRUE) == TRUE)
    {
      shmdt(lemipc->map);
      usleep(SLEEP);
      semctl(lemipc->sem_id, 0, IPC_RMID);
      shmctl(lemipc->shm_id, IPC_RMID, NULL);
      msgctl(lemipc->msg_id, IPC_RMID, NULL);
    }
  else
    shmdt(lemipc->map);
  return (EXIT_SUCCESS);
}
Beispiel #9
0
void start_game()
{
	// p==1 then X   p==0  then  O
	check_draw();
	move_to(getPossibleWinningPos());
	draw_board();

	if (isWinning(comp))
	{
		gotoxy(30, 20);
		SetConsoleTextAttribute(h, FOREGROUND_BLUE);
		printf("Computer wins\n");
		SetConsoleTextAttribute(h, 7); //7 - WHITE
		_getch();
		menu();
	}
	else
		player_turn();
}
Beispiel #10
0
void	on_attack(int pos)
{
  int	x;
  int	y;

  y = pos / 8;
  x = pos % 8;
  if (GAME.navy_positions[y][x] >= '1' && GAME.navy_positions[y][x] <= '8')
    {
      GAME.navy_positions[y][x] = 'x';
      on_attack_hit(pos, 0);
      send_packet(CS_ATTACK_HIT, pos);
    }
  else
    {
      if (GAME.navy_positions[y][x] != 'x')
	GAME.navy_positions[y][x] = 'o';
      on_attack_miss(pos, 0);
      send_packet(CS_ATTACK_MISS, pos);
    }
  test_win();
  player_turn();
}
Beispiel #11
0
bool play(bool with_ai)
{
	char grid[SIZE][SIZE];
	Index2d pos;

	int moves = 0;
	int max_moves = SIZE * SIZE;

	static int player1_score = 0, player2_score = 0;

	srand((unsigned)time(NULL));
	rand();

	/* Initialize grid */
	memset(grid, ' ', SIZE * SIZE);

	char player1 = rand() % 2 ? 'x' : 'o';
	char player2 = (player1 == 'x' ? 'o' : 'x');

	if (with_ai) {
		printf("You are %c\n", player1);
		if (player1 == 'x')
			print_grid(grid);
	}
	else {
		printf("Player 1 is %c. Player 2 is %c\n", player1, player2);
		print_grid(grid);
	}

	if (player1 == 'o') {
		if (with_ai)
			ai_turn(grid, player1, player2);
		else {
			puts("Player 2's turn");
			player_turn(grid, player2);
		}
		++moves;
		print_grid(grid);
	}

	for (;;) {
		/* Player 1 */
		if (moves < max_moves) {
			if (!with_ai)
				puts("Player 1's turn");
			pos = player_turn(grid, player1);
			++moves;

			if (won(grid, pos, player1)) {
				if (with_ai)
					puts("You won");
				else
					puts("Player 1 won");
				++player1_score;
				break;
			}

			/* Don't print the grid before the AI's turn */
			if (!with_ai)
				print_grid(grid);
		}

		/* Player 2 / AI */
		if (moves < max_moves) {
			if (with_ai)
				pos = ai_turn(grid, player1, player2);
			else {
				puts("Player 2's turn");
				pos = player_turn(grid, player2);
			}
			++moves;

			if (won(grid, pos, player2)) {
				if (with_ai)
					puts("You lost");
				else
					puts("Player 2 won");
				++player2_score;
				break;
			}

			print_grid(grid);
		}

		if (moves == max_moves) {
			puts("Tie");
			break;
		}
	}

	print_grid(grid);
	printf("Player 1  Player 2\n"
		"--------  --------\n"
		"%8d%10d\n", player1_score, player2_score);

	return prompt_bool("Play again?", true);
}
Beispiel #12
0
void input_movemouse (SDL_MouseMotionEvent *mm)
{
	player_turn (&player, mm->xrel);
}
Beispiel #13
0
int main(int argc, char **argv)
{
	int i, cc;
	char msg[MSG_BFR_SZ];
	char tag[MSG_BFR_SZ];

	struct player_data *p = NULL;

	--argc; ++argv;
	setbuf(stdout, NULL);
	setbuf(stdin , NULL);

	if (!client_setup(&argc, &argv))
		return EXIT_FAILURE;

	recv(msg); sscanf(msg, "%*s %d", &SELF.id);
	sprintf(msg, "NAME %s", BOT_NAME); send(msg);

	while ((cc = recv(msg)))
	{
		sscanf(msg, "%s", tag);
		
		if (!strcmp(tag, "READY")) break;
		else if (!strcmp(tag, "PLAYERS"))
		{
			sscanf(msg, "%*s %u", &numplayers);
			for (i = 0, p = players; i < numplayers; ++i, ++p)
			{
				recv(msg); sscanf(msg, "%u %u", &p->id, &p->pool);
				p->wager = p->card = p->active = 0;
			}
		}
		else if (!strcmp(tag, "CARDS"))
			sscanf(msg, "%*s %u %u", &XRANGE, &XDUP);
		else if (!strcmp(tag, "ANTE"));
			sscanf(msg, "%*s %*d %u", &ROUNDS_TO_DBL);
	}

	if (!p) EXPECTED(tag, "PLAYERS");
	copyself(); game_setup(players, numplayers);

	while ((cc = recv(msg)))
	{
		sscanf(msg, "%s", tag);
		
		if (!strcmp(tag, "ENDGAME")) break;
		else if (!strcmp(tag, "ROUND"))
		{
			unsigned int rnum, pstart, rante;
			sscanf(msg, "%*s %u %u %u", &rnum, &pstart, &rante);
			copyself(); round_start(rnum, pstart, rante);

			while ((cc = recv(msg)))
			{
				sscanf(msg, "%s", tag);
				if (!strcmp(tag, "TURN"))
				{
					for (i = 0, p = players; i < numplayers; ++i, ++p)
					{
						recv(msg);
						sscanf(msg, "%u %u %u %u %u", &p->id, &p->card, &p->pool, 
							&p->wager, &p->active);
					}

					copyself(); recv(tag);
					if (strcmp(tag, "GO")) EXPECTED(tag, "GO");
					int k = player_turn(players, numplayers);

					// perform the chosen action
					switch (k)
					{
						case CALL: sprintf(msg, "CALL"); break;
						case FOLD: sprintf(msg, "FOLD"); break;
						default:   sprintf(msg, "WAGER %d", k); break;
					}
					send(msg);
				}
				else if (!strcmp(tag, "ENDROUND"))
				{
					int winnings; sscanf(msg, "%*s %u", &winnings);
					for (i = 0, p = players; i < numplayers; ++i, ++p)
					{
						recv(msg); p->wager = p->active = 0;
						sscanf(msg, "%u %u %u", &p->id, &p->card, &p->pool);
					}
					copyself(); round_end(players, numplayers, winnings);
					break;
				}
				// got an unexpected message...
				else EXPECTED(tag, "TURN/ENDROUND");
			}
		}
		// got an unexpected message...
		else EXPECTED(tag, "ROUND/ENDGAME");
	}

	game_end();
	return EXIT_SUCCESS;
}
Beispiel #14
0
int main(int argc, char **argv)
{
	int i, cc;
	char msg[MSG_BFR_SZ];
	char tag[MSG_BFR_SZ];

	struct player_data *p = NULL;

	--argc; ++argv;
	setbuf(stdout, NULL);
	setbuf(stdin , NULL);
	
	struct timeval tv;
	gettimeofday(&tv, NULL);
	if (!client_setup(&argc, &argv))
		return EXIT_FAILURE;

	recv(msg); sscanf(msg, "%*s %d", &SELF.id);
	sprintf(msg, "NAME %s", BOT_NAME); send(msg);
	
	srand(tv.tv_usec+SELF.id);
	while ((cc = recv(msg)))
	{
		sscanf(msg, "%s", tag);
		
		if (!strcmp(tag, "READY")) break;
		else if (!strcmp(tag, "BOARD"))
			sscanf(msg, "%*s %u", &BOARDSIZE);
		else if (!strcmp(tag, "PLAYERS"))
		{
			sscanf(msg, "%*s %u", &NUMPLAYERS);
			for (i = 0, p = players; i < NUMPLAYERS; ++i, ++p)
			{
				p->id = i;
				p->lastscore = p->score = 0;
			}
		}
		else if (!strcmp(tag, "ROUNDS"))
			sscanf(msg, "%*s %u", &NUMROUNDS);
	}

	copyself(); game_setup(players);

	unsigned int rnum;
	while ((cc = recv(msg)))
	{
		sscanf(msg, "%s", tag);
		
		if (!strcmp(tag, "ENDGAME")) break;
		else if (!strcmp(tag, "ROUND"))
		{
			unsigned int rnum;
			sscanf(msg, "%*s %u", &rnum);
			
			while ((cc = recv(msg)))
			{
				sscanf(msg, "%s", tag);
				
				if (!strcmp(tag, "GO")) break;
				else EXPECTED(tag, "GO");
			}

			copyself();
			player_turn(rnum, players);

			clamp(&SELF.cow.x, 0, BOARDSIZE);
			clamp(&SELF.cow.y, 0, BOARDSIZE);

			clamp(&SELF.fence1.x, 0, BOARDSIZE);
			clamp(&SELF.fence2.x, SELF.fence1.x, BOARDSIZE);

			clamp(&SELF.fence1.y, 0, BOARDSIZE);
			clamp(&SELF.fence2.y, SELF.fence1.y, BOARDSIZE);

			sprintf(msg, "MOVE %u %u %u %u %u %u"
			,	SELF.cow.x, SELF.cow.y
			,	SELF.fence1.x, SELF.fence1.y
			,	SELF.fence2.x, SELF.fence2.y
			);
			send(msg);
			
			while ((cc = recv(msg)))
			{
				sscanf(msg, "%s", tag);
				
				if (!strcmp(tag, "NEXT")) break;
				else if (!strcmp(tag, "PLAYER"))
				{
					sscanf(msg, "%*s %d", &i);
					p = &players[i];

					sscanf(msg, "%*s %*d %u %u %u %u %u %u %u %u"
					,	&p->cow.x, &p->cow.y
					,	&p->fence1.x, &p->fence1.y
					,	&p->fence2.x, &p->fence2.y
					,	&p->lastscore, &p->score
					);
				}
			}
		}
		
		// got an unexpected message...
		else EXPECTED(tag, "ROUND/ENDGAME/MOVE/UPDATE");
	}

	quit: game_end();
	return EXIT_SUCCESS;
}