예제 #1
0
int main(void)
{
  char done;

  printf("This is the game of Tic Tac Toe.\n");
  printf("You will be playing against the computer.\n");

  done =  ' ';
  init_matrix();

  do {
    disp_matrix();
    get_player_move();
    done = check(); /* see if winner */
    if(done!= ' ') break; /* winner!*/
    get_computer_move();
    done = check(); /* see if winner */
  } while(done== ' ');

  if(done=='X') printf("You won!\n");
  else printf("I won!!!!\n");
  disp_matrix(); /* show final positions */

  return 0;
}
예제 #2
0
파일: COMP2.CPP 프로젝트: pjmodi/projects
void test(void)		//Checks for a winner or continues the game
{
//X's winning conditions
if (
	(t[1] == 'X' && t[2] == 'X' && t[3] == 'X') ||
	(t[4] == 'X' && t[5] == 'X' && t[6] == 'X') ||
	(t[7] == 'X' && t[8] == 'X' && t[9] == 'X') ||
	(t[1] == 'X' && t[4] == 'X' && t[7] == 'X') ||
	(t[2] == 'X' && t[5] == 'X' && t[8] == 'X') ||
	(t[3] == 'X' && t[6] == 'X' && t[9] == 'X') ||
	(t[1] == 'X' && t[5] == 'X' && t[9] == 'X') ||
	(t[3] == 'X' && t[5] == 'X' && t[7] == 'X')
   )
	{cout << "\n\nPlayer 1 WINS!!";}

//O's winning conditions
else if (
	(t[1] == 'O' && t[2] == 'O' && t[3] == 'O') ||
	(t[4] == 'O' && t[5] == 'O' && t[6] == 'O') ||
	(t[7] == 'O' && t[8] == 'O' && t[9] == 'O') ||
	(t[1] == 'O' && t[4] == 'O' && t[7] == 'O') ||
	(t[2] == 'O' && t[5] == 'O' && t[8] == 'O') ||
	(t[3] == 'O' && t[6] == 'O' && t[9] == 'O') ||
	(t[1] == 'O' && t[5] == 'O' && t[9] == 'O') ||
	(t[3] == 'O' && t[5] == 'O' && t[7] == 'O')
    )
	{cout << "\n\nPlayer 2 WINS!!";}

//Draw conditions
else if (count == 10)
		{
		cout << "\n\nIt's a Draw! Please try again.";
		}

//For alternate turns of the 2 players
else    {
	if (count%2 != 0)
		{
		cout << "\n\nPlayer 2's turn. ";
		ch1='X';
		get_computer_move();
		t[ip]='X';
		}
	else
		{
		cout << "\n\nPlayer 1's turn. ";
		ch2='O';
		get();
		convert();
		t[ip]='O';
		readip=ip;
		}
	}
}
예제 #3
0
/* Calls minimax to get AIs next move */
int	C4_handle_computer_turn(int* game_state, int depth, int player)
{
	int comp_move;
	if (player==-1){
		comp_move = get_computer_move(game_state, depth, get_state_children_C4);
	}
	else {
		comp_move=get_suggested_move(game_state,depth, get_state_children_C4);
	}
	if(comp_move<0){
		return -1;
	}
	else if (comp_move==0){//zero cann't be valid move in this game
		return -2;
	}
	C4_make_move(game_state,0,comp_move-1,player);//return 0 doesn't mean error??
	return 0;
}
예제 #4
0
파일: go11.c 프로젝트: KitAiProject/Go
void selfplay()
{
  int color = 1;
  int z,search;
  
  for (;;) {
    if ( color == 1 ) {
      search = SEARCH_UCT; //SEARCH_PRIMITIVE;
    } else {
      search = SEARCH_UCT;
    }
    z = get_computer_move(color, search);
    add_moves(z, color);
    if ( z == 0 && moves > 1 && record[moves-2] == 0 ) break;
    if ( moves > 300 ) break;  // too long
    color = flip_color(color);
  }

  print_sgf();
}
예제 #5
0
//int main(int argc, char *argv[], char *env[])
void main(void)
{
 int i, j;
 mvtype movemade;
 mousex=100;
 mousey=100;
 iconmenu=0;
 set_default_options();
 options=doptions;
 read_options();
 initialise(startbrd);
 initial_displays(current);
 no_more_openings=FALSE;
 quit=game_over=0;
 g_path.move[0].f=0;
 randomize();
 FLAGnewstart=1;
 while(!quit)
 {
   while(game.movenum<maxmoves && !quit)
   {
	 whites=0;
	 blacks=0;
	 if (FLAGnewstart) { starttime=time(NULL); FLAGnewstart=0; }
	 newtime=0;
	 if (!game_over && ((current.mvr=='w' && options.whiteplayer==PROGRAM) || (current.mvr=='b' && options.blackplayer==PROGRAM)))
	      movemade=get_computer_move(current); else
		 movemade=get_player_move(current, game_over);
	 if (kbhit()) { if (getch()==27) { options.whiteplayer=USER; options.blackplayer=USER; } }
	 if (current.mvr=='w') (whites=time(NULL)-starttime); else (blacks=time(NULL)-starttime);
	 if (movemade.t!=0) if (movemade.t<100) makemv(movemade); else {menu_option(movemade.t); game_over=0;}
	 regular_displays(current, movemade);
	 if (!game_over) game_over=over();
	 assert(game.lastmove<maxmoves);
   }
   options.whiteplayer=USER;
   options.blackplayer=USER;
 }
}
예제 #6
0
파일: go11.c 프로젝트: KitAiProject/Go
void gtp_loop()
{
  char str[STR_MAX];
  char sa[TOKEN_MAX][STR_MAX];
  char seps[] = " ";
  char *token;
  int x,y,z,ax, count;

  setbuf(stdout, NULL);
  setbuf(stderr, NULL);
  for (;;) {
    if ( fgets(str, STR_MAX, stdin)==NULL ) break;
//  prt("gtp<-%s",str);
    count = 0;
    token = strtok( str, seps );
    while ( token != NULL ) {
      strcpy(sa[count], token);
      count++;
      if ( count == TOKEN_MAX ) break;
      token = strtok( NULL, seps );
    }

    if ( strstr(sa[0],"boardsize")     ) {
//    int new_board_size = atoi( sa[1] );
      send_gtp("= \n\n");
    } else if ( strstr(sa[0],"clear_board")   ) {
      init_board();
      send_gtp("= \n\n");
    } else if ( strstr(sa[0],"quit") ) {
      break;
    } else if ( strstr(sa[0],"protocol_version") ) {
      send_gtp("= 2\n\n");
    } else if ( strstr(sa[0],"name")          ) {
      send_gtp("= your_program_name\n\n");
    } else if ( strstr(sa[0],"version")       ) {
      send_gtp("= 0.0.1\n\n");
    } else if ( strstr(sa[0],"list_commands" ) ) {
      send_gtp("= boardsize\nclear_board\nquit\nprotocol_version\n"
               "name\nversion\nlist_commands\nkomi\ngenmove\nplay\n\n");
    } else if ( strstr(sa[0],"komi") ) {
      komi = atof( sa[1] );
      send_gtp("= \n\n");
    } else if ( strstr(sa[0],"genmove") ) {
      int color = 1;
      if ( tolower(sa[1][0])=='w' ) color = 2;

      z = get_computer_move(color, SEARCH_UCT);
      add_moves(z, color);
      send_gtp("= %s\n\n",get_char_z(z));
    } else if ( strstr(sa[0],"play") ) {  // "play b c4", "play w d17"
      int color = 1;
      if ( tolower(sa[1][0])=='w' ) color = 2;
      ax = tolower( sa[2][0] );
      x = ax - 'a' + 1;
      if ( ax >= 'i' ) x--;
      y = atoi(&sa[2][1]);
      z = get_z(x, B_SIZE-y+1);
      if ( tolower(sa[2][0])=='p' ) z = 0;  // pass
      add_moves(z, color);
      send_gtp("= \n\n");
    } else {
      send_gtp("? unknown_command\n\n");
    }
  }
}
예제 #7
0
파일: COMP2.CPP 프로젝트: pjmodi/projects
/* Get the computer's move */
void get_computer_move(void)
{
check=0;
block();
if(check==0)
	{
	if (count==1)
	ip=2;
	else if(count==3)
		{
		if(t[1]=='O' || t[3]=='O' || t[5]=='O' || t[8]=='O')
		  ip=9;
		else if(t[4]=='O' || t[6]=='O')
		ip=5;
		else if(t[7]=='O')
		ip=1;
		else if(t[9]=='O')
		ip=3;
		}
	else if(count==5)
		{
		switch(readip)
			{
			case 7: if(t[1]=='O')
				  ip=5;
				else if(t[3]=='O')
				  ip=5;
				else if(t[5]=='O')
				  ip=3;
				else if(t[8]=='O')
				  ip=3;
				else do
					{
					ip=random(10);
					}while(ip==0);
				break;
			case 8: if(t[4]=='O' || t[6]=='O')
				  ip=3;
				else do
					{
					ip=random(10);
					}while(ip==0);
				break;
			case 3: if(t[7]=='O')
				  ip=5;
				else if(t[1]=='O')
				  ip=8;
				else do
					{
					ip=random(10);
					}while(ip==0);
				break;
			case 1: ip=5;
				break;
			case 4: if(t[8]=='O')
					  ip=3;
				       else do
					{
					ip=random(10);
					}while(ip==0);
				break;
			case 5: if(t[8]=='O')
					  ip=3;
				       else do
					{
					ip=random(10);
					}while(ip==0);
				break;
			case 6: if(t[8]=='O')
				  ip=1;
				else do
					{
					ip=random(10);
					}while(ip==0);
				break;
			default:do
				{
				ip=random(10);
				}while(ip==0);
				break;
			}
		}
	else if (count==7)
		{
		do
			{
			ip=random(10);
			}
		while(ip==0);
		}
	else if(count==9)
		{
		for (int i=1;i<=9;i++)
			{
			itoa(i,ic,10);
			if(t[i]==ic[0])
				{
				ip=i;
				break;
				}
			}
		}
	}

itoa(ip,c,10);

for (int i=1; i<=10; i++)       //Dis-allows duplicate entries
     {
	if (c[0]==b[i])
		{
		get_computer_move();
		}
     }
}