Пример #1
0
int main()
{
// the first local variables
   int counter = 0;
   bool winner = false;
   
   srand(GetTickCount());   //sets randomizer as time *unique*
   // get TickCount returns a semi random number for feeding into the srand function

   cout << "Please select a number from 1-7" << endl;
   cout << "| 1| 2| 3| 4| 5| 6| 7" << endl;
   cout << "_____________________";
   
   // even through function hasn't been described in the code, the compiler knows to look further down here (possibly looking for angle brackets) 
   display_board();

   // MAIN GAME LOOP
   // 42 spaces (6 by 7 array), divded by 2 players == maximumm no. of rounds
   for (int i = 0; i < 21; i++)
   {
      player_movement(1);
      display_board();
      winner = check_for_winner(LastMoveX, LastMoveY, 1);
      if (winner)
      {
         cout << "\nYou Win" << endl;
         break;
      }
   
      player_movement(2);
      display_board();
      winner = check_for_winner(LastMoveX, LastMoveY, 2);
      if (winner)
      {
         cout << "\nYou Win" << endl;
         break;
      }   
   }
   
   system("PAUSE");
   return 0;
}
Пример #2
0
int main()
{
  int x = rand_num() % 10;
  int y = rand_num() % 10;
  int z = rand_num() % 10;
  char column;
  int num_of_discs = 0;
  char winner = 'n';
  char newline = '\n';
  char play_again = 'y';

  while(play_again == 'y')
    {
      x = rand_num() % 10;
      y = rand_num() % 10;
      z = rand_num() % 10;
      num_of_discs = 0;
      winner = 'n';

      print_board(x, y, z);
      
      while(winner == 'n')
	{
	  //Player 1's turn
	  do 
	    {
	      //get input
	      printf("Your Turn. Enter column, number: ");
	      scanf("%c, %d%c", &column, &num_of_discs, &newline);
	      printf("\n");
	      //filter input
	      if(column != 'x' && column != 'y' && column != 'z') 
		printf("Not a valid column! Please try again.\n");
	      if(num_of_discs < 1)
		printf("Not enough discs! Please try again.\n");
	      if(num_of_discs > 9)
		printf("Too many discs! Please try again.\n");
	      if( is_empty(x, y, z, column) )
		printf("Column %c is empty. Please try again.\n", column);
	    } 
	  while( (column != 'x' && column != 'y' && column != 'z' ) 
		 || num_of_discs < 1 || num_of_discs > 9 
		 || is_empty(x, y, z, column) );
	  
	  //take discs
	  switch(column)
	    {
	    case 'x':
	      x = x - num_of_discs;
	      break;
	    case 'y':
	      y = y - num_of_discs;
	      break;
	    case 'z':
	      z = z - num_of_discs;
	      break;
	    }
	  
	  if( check_for_winner(x, y, z) )
	    {
	      winner = '1';
	      printf("Player 1 is the winner!\n");
	      break;
	    }
	  
	  
	  print_board(x, y, z);
	  
	  
	  //Computer's turn
	  do
	    {
	      column = computer_select_column(x, y, z);
	    }
	  while(is_empty(x, y, z, column));
	  num_of_discs = computer_num_of_discs(x, y, z, column);
	  //take discs
	  switch(column)
	    {
	    case 'x':
	      x = x - num_of_discs;
	      break;
	    case 'y':
	      y = y - num_of_discs;
	      break;
	    case 'z':
	      z = z - num_of_discs;
	      break;
	    } 
	  printf("Computer's takes %d from %c\n", num_of_discs, column);
	  
	  if( check_for_winner(x, y, z) )
	    {
	      winner = 'c';
	      printf("Computer is the winner!\n");
	      break;
	    }

	  print_board(x, y, z);
	}
      
      print_board(x, y, z);

      printf("Would you like to play again?(y or n): ");
      scanf("%c", &play_again);
    }

  return(0);
}