示例#1
0
void control(gamedata &g, Uint8 *keys, int &jx, int &jy, bool &jb){

  switch(g.p().control){
    case 1: // Player 1
      if(demo)
	computer_ai(g, g.p(), jx, jy, jb);
      else
      	keyboard(keys, jx, jy, jb, SDLK_UP, SDLK_LALT,
               SDLK_LEFT, SDLK_RIGHT, SDLK_LCTRL);
      break;
    case 2: // Player 2
      if(demo)
	computer_ai(g, g.p(), jx, jy, jb);
      else
      	keyboard(keys, jx, jy, jb, SDLK_s, SDLK_x,
               SDLK_z, SDLK_c, SDLK_LCTRL);
      break;
    default: // Computer controlled
      computer_ai(g, g.p(), jx, jy, jb);
      break;
  }

}
示例#2
0
int human_predictor(int board[3][3], int level, int moves) {
    int wincheck, x, y, placesuccess, noderootmin, bestnoderootmin, numvalues=0, p, q;
    int boardcopy[3][3] = { { 0 } };
    int values[3][9] = { { 0 } };

    for (x = 0; x < 3; x++) {
        for (y = 0; y < 3; y++) {
            for (p = 0; p < 3; p++) {
                for (q = 0; q < 3; q++) {
                    boardcopy[p][q] = board[p][q];
                }
            }

            placesuccess = placeitem(0, x, y, boardcopy, 1, 0);

            if (placesuccess == 0) {
                wincheck = checkwin(boardcopy);
                if (wincheck == 0) {
                    return -1;
                } else if (wincheck == -1) {
                    if (moves + 1 == 9) {
                        return 0;
                    }
                    noderootmin = 0;
                    noderootmin = computer_ai(boardcopy, level + 1, moves + 1);
                    if (noderootmin < 100) {
                        values[0][numvalues] = noderootmin;
                        values[1][numvalues] = x;
                        values[2][numvalues] = y;
                        numvalues++;
                    }
                }
            }
        }
    }

    bestnoderootmin = values[0][0];
    for (x = 0; x < numvalues-1; x++) {
        if (values[0][x+1] < values[0][x]) {
            bestnoderootmin = values[0][x+1];
        }
    }
    return bestnoderootmin;
}
示例#3
0
int main() {
    int player = 0, moves = 0, end = -1, x, y, successplace;
    int board[3][3] = { { 0 } };

    do {
        successplace = 1;
        printf("Player %d's turn\n", player + 1);

        if (player == 1) {
            x = computer_ai(board, 1, moves);
        } else {
            do {
                x = getcoord(0); /* input and checks bounds */
                y = getcoord(1);
                successplace = placeitem(player, x-1, y-1, board, 0, 1);
            } while (successplace == 1);
        }

        printmap(board);

        if (moves > 3) {
            end = checkwin(board);
        }

        moves++;
        player = (player + 1) % 2;
    } while (end == -1 && moves < 9);

    if (end == -1) {
        printf("Draw\n");
    } else {
        printf("Player %d wins!\n", end + 1);
    }

    return EXIT_SUCCESS;
}
示例#4
0
void controlJoy(gamedata &g, int &jx, int &jy, bool &jb){
	int deadzone = 250;
	int x;
	int y;

	switch(g.p().control){
		case 1:
		{
			if(g.playerJoy[0] == -1)
				break;

			SDL_Joystick *joy = SDL_JoystickOpen(g.playerJoy[0]);
			if(demo)
			{
				//computer_ai(g, g.p(), jx, jy, jb);
			}
			else
			{
				x = SDL_JoystickGetAxis(joy, 0); // left-right
				y = SDL_JoystickGetAxis(joy, 1); // up-down

				if(x < -deadzone)
					jx = -1;
				else if(x > deadzone)
					jx = 1;
				//else
				//	jx = 0;


				if(y < -deadzone)
					jy = -1;
				//else if(y > deadzone)
				//	jy = 1; // drop bomb
				//else
				//	jy = 0;

				if(!jb)
					jb = (bool)SDL_JoystickGetButton(joy, g.playerJoyBut[0][0]); // fire
				if(SDL_JoystickGetButton(joy, g.playerJoyBut[0][1])) // drop bomb
					jy = 1;

				if(SDL_JoystickGetButton(joy, g.playerJoyBut[0][2]) && GameState == STATE_GAME)
				{
					GameState = STATE_MENU;
					CurrentMenu = MenuMain;
					SelectedItem = menuSwitchItem(CurrentMenu, 0);
				}
			}
		}
			break;
		case 2:
		{
			if(g.playerJoy[1] == -1)
				break;

			SDL_Joystick *joy = SDL_JoystickOpen(g.playerJoy[1]);
			if(demo)
			{
				//computer_ai(g, g.p(), jx, jy, jb);
			}
			else
			{
				x = SDL_JoystickGetAxis(joy, 0); // left-right
				y = SDL_JoystickGetAxis(joy, 1); // up-down

				if(x < -deadzone)
					jx = -1;
				else if(x > deadzone)
					jx = 1;
				//else
				//	jx = 0;


				if(y < -deadzone)
					jy = -1;
				//else if(y > deadzone)
				//	jy = 1; // drop bomb
				//else
				//	jy = 0;

				if(!jb)
					jb = (bool)SDL_JoystickGetButton(joy, g.playerJoyBut[1][0]); // fire
				if(SDL_JoystickGetButton(joy, g.playerJoyBut[1][1])) // drop bomb
					jy = 1;
			}
		}
			break;

		default:
			computer_ai(g, g.p(), jx, jy, jb);
		break;
	}
}