Exemplo n.º 1
0
void horizontal_row0(CuTest* tc)
{
  int num_rows = 7;
  int num_columns = 7;
  int array[num_rows][num_columns];
  int answer;
  ct_initialize(num_rows, num_columns, array);

  placeToken(0, 0, num_rows, num_columns, array);
  answer = winner(num_rows, num_columns, 4, array);
  CuAssertIntEquals_Msg(tc, "Single 0 in column 0", NO_WINNER_YET, answer);

  placeToken(0, 1, num_rows, num_columns, array);
  answer = winner(num_rows, num_columns, 4, array);
  CuAssertIntEquals_Msg(tc, "0s in columns {0,1}", NO_WINNER_YET, answer);


  placeToken(0, 2, num_rows, num_columns, array);
  answer = winner(num_rows, num_columns, 4, array);
  CuAssertIntEquals_Msg(tc, "0s in columns {0,1, 2}", NO_WINNER_YET, answer);


  placeToken(0, 3, num_rows, num_columns, array);
  answer = winner(num_rows, num_columns, 4, array);
  CuAssertIntEquals_Msg(tc, "4 in a row, horizontal", 0, answer);
}
int main()
{
int tmp = 0;
    while(cin.peek()!=EOF)
    {
//cout << tmp++ << endl;
        clearCards(black);
        clearCards(white);
        getCards(black);
        getCards(white);
//displayCards(white);
//displayCards(black);
        switch(winner())
        {
            case 1:
                cout << "Black wins." << endl;
                break;
            case -1:
                cout << "White wins." << endl;
                break;
            case 0:
                cout << "Tie." << endl;
                break;
            default:
                cout << "bad value: " << winner() << endl;
        }
//cout << endl << endl << endl;
    while(cin.peek()=='\n')cin.get();
    }
    //system("PAUSE");
    return 0;
}
Exemplo n.º 3
0
void vertical_column1(CuTest* tc)
{
  int num_rows = 7;
  int num_columns = 7;
  int array[num_rows][num_columns];
  int answer;
  ct_initialize(num_rows, num_columns, array);

  placeToken(0, 1, num_rows, num_columns, array);
  answer = winner(num_rows, num_columns, 4, array);
  CuAssertIntEquals_Msg(tc, "Single 0 in column 1", NO_WINNER_YET, answer);

  placeToken(0, 1, num_rows, num_columns, array);
  answer = winner(num_rows, num_columns, 4, array);
  CuAssertIntEquals_Msg(tc, "Two 0s in column 1", NO_WINNER_YET, answer);


  placeToken(0, 1, num_rows, num_columns, array);
  answer = winner(num_rows, num_columns, 4, array);
  CuAssertIntEquals_Msg(tc, "Three 0s in column 1", NO_WINNER_YET, answer);


  placeToken(0, 1, num_rows, num_columns, array);
  answer = winner(num_rows, num_columns, 4, array);
  CuAssertIntEquals_Msg(tc, "4 in a row, vertical", 0, answer);
}
Exemplo n.º 4
0
void WinnerTree::play(int p, int lc, int rc){
    m_tree[p] = winner(lc, rc);
    //continue competition
    while( p > 1 && p % 2) {
	m_tree[p/2] = winner( m_tree[p - 1], m_tree[p]);
	p/=2;
  }
}
int TicTacToeState::utility(unsigned int depth)
{
    // Only support X is human, 
    // O is computer now
    if (winner() == p1_symbol) 
        return 10 - (int)depth;
    else if (winner() == p2_symbol)
        return (int)depth - 10;
    // If it is a tie
	return 0;
}
Exemplo n.º 6
0
void playerInterface(char** tab, int nbColumns, int nbLines,int noGui, int log,int megafunModeOption)
{
  int i=0,x,player=1,choice,win=0;
  char cChoice[2];
  int nbBombs[2]={3,3};
  struct Coord Coord;
  do
    {
      if(!noGui)
	displayGrid(tab,nbLines,nbColumns);
      if(i%2==0)
	{
	  player=1;
	  Coord.bawn='X';
	}
      else
	{
	  player=2;
	  Coord.bawn='O';
	}
      if(megafunModeOption)
	bombInterface(tab,nbColumns,nbLines,&nbBombs[player-1],log,noGui);
      printf("\nJoueur %d, choisissez une colonne dans laquelle insérer votre pion:",player);
      if(read(cChoice,2,&choice)==0)
	printf("Problème dans la fonction read!!!!!");
      if(choice<0 || choice>nbColumns-1) //si choix fantaisiste
	{
	  printf("Veuillez choisir une colonne entre 0 et %d\n",nbColumns-1);
	  ++i;//on incrémente i deux fois afin que ce soit le même joueur qui rejoue
	}
      else
	{
	  x=placeBawn(tab,nbLines,nbColumns,choice,&Coord);
	  win=winner(tab, nbLines, nbColumns,&Coord);
	  if(log)
	    logFunction(&Coord,i,0,player,x);
	  if(x==-1)
	    ++i;
	}
      system("clear");
      if(megafunModeOption)
	megafunMode(tab,nbLines,nbColumns,&i,log);
      i++;
    }while(win==0);
  if(!noGui)
    displayGrid(tab,nbLines,nbColumns);
  if(winner(tab,nbLines,nbColumns,&Coord)==1)
    printf("\nLe joueur %d gagne la partie!!\n",player);
  else 
    printf("\nEgalité!\n");
  if(log)
    logFunction(&Coord,i,1,player,x);
}
Exemplo n.º 7
0
 int winner (vector<int>& nums, int start, int end, int turn, int sum)
 {
     if (start == end)
     {
         return turn * nums[start] + sum; 
     }
     else
     {
         int res1 = winner(nums, start + 1, end, 0 - turn, sum + turn * nums[start]);
         int res2 = winner(nums, start, end - 1, 0 - turn, sum + turn * nums[end]);
         return turn * max(turn * res1, turn * res2);
     }
 }
Exemplo n.º 8
0
void play(char win[], char j1[], char j2[],int *valMatches)//
{
    int tpm;
    int allumette=*valMatches;
    tpm=rand()%2; /*Randomly chooses the player who starts the game*/
    matches_21(X,Y,1,allumette,10);
    printf("\n\n\n\n\t\t\t\t%s vs %s ... Ready ... Fight !!!  (Press Enter)",j1,j2);
    getch();
    clrScr();
    matches_21(X,Y,1,allumette,10);
    setColour(ROUGE_VIF,NOIR);
    printf("\n\n\n\n\n\t\t\t\t\t#");
    setColour(BLANC,NOIR);
    printf(" There are");
    setColour(MAGENTA_VIF,NOIR);
    printf(" %d ",allumette);
    setColour(BLANC,NOIR);
    printf("matches on table ");
    setColour(ROUGE_VIF,NOIR);
    printf("#\n");
    setColour(BLANC,NOIR);
    do
    {
        allumette=game(allumette,tpm,win,j1,j2);
        tpm=change(tpm);
    }while(allumette>1);
    printf("\n\n\n\t\t\t\t\t    The Game is Finished !!!\n");
    winner(win,allumette);
}
int main(int argc, char* argv[]) {
	
	board_t* board = create_board();
	symbol_t result;
	symbol_t current_symbol = X;

	move_t m;
 	m.i = 1;
	m.j = 1;
	//put_symbol(board, 0, &m);

//	print_board(board);

	while(1) {
		printf("Player %i to move next\n", (int) current_symbol);
		move(board, current_symbol, 0, -9999, 9999);
	//	print_board(board);
		result = winner(board);
	
		if(result != EMPTY) {
			break;
		}

		current_symbol = 1 - current_symbol;
	}

	printf("Winner: %i\n:", (int) result);

	return 0;
}
Exemplo n.º 10
0
int main(int argc, char *argv[3]) {
    if (argc < 3) {
        printf("Not enough inputs!\n");
        return -1;
    }
    int game_in_session = 1;
    int cell_dimen = atoi(argv[1]);
    int length_to_win = atoi(argv[2]);
    int current_player = 0;
    int column;
    int win;
    int board[cell_dimen][cell_dimen];
    printf("Board size: %d x %d, To win: %d\n",
           cell_dimen, cell_dimen, length_to_win);
    initialize(cell_dimen, cell_dimen, board);
    do {
        printf("Ready player %d\n", current_player + 1);
        printf("Enter column: ");
        check_valid_input(&column);
        int placed = place_token(current_player, column, cell_dimen, cell_dimen, board);
        if (placed < 0) {
            printf("\nCouldn't place token at column %d\n\n", column);
            continue;
        }
        win = winner(cell_dimen, cell_dimen, length_to_win, board);
        print_board(cell_dimen, cell_dimen, board);
        current_player = current_player == 0 ? 1 : 0;
        if (win >= 0) {
            game_in_session = 0;
            break;
        }
    } while (game_in_session);
    print_winner(win);
    return 0;
}
Exemplo n.º 11
0
//Resolves the battle at planet p, if there is one.
//* Removes all fleets involved in the battle
//* Sets the number of ships and owner of the planet according the outcome
void GameState::__FightBattle(PlanetState& p) {
	std::map<int,int> participants;	
	participants[p.owner] = p.numShips;
	
	for (Fleets::iterator it = fleets.begin(); it != fleets.end(); ) {
		Fleet& f = *it;
		if (f.turnsRemaining <= 0 && &planets[f.destinationPlanet] == &p) {
			participants[f.owner] += f.numShips;
			it = fleets.erase(it);
		}
		else ++it;
	}
	
	Fleet winner(0, 0);
	Fleet second(0, 0);
	for (std::map<int,int>::iterator f = participants.begin(); f != participants.end(); ++f) {
		if (f->second > second.numShips) {
			if(f->second > winner.numShips) {
				second = winner;
				winner = Fleet(f->first, f->second);
			} else {
				second = Fleet(f->first, f->second);
			}
		}
	}
	
	if (winner.numShips > second.numShips) {
		p.numShips = winner.numShips - second.numShips;
		p.owner = winner.owner;
	} else {
		p.numShips = 0;
	}
}
Exemplo n.º 12
0
static void MakeMoveCB(Widget, XtPointer client_data, XtPointer)
{
    if (winner() != NO_ONE)
	initBoard();

    make_move((int)(long)client_data);
}
Exemplo n.º 13
0
void ifdraw()                //Checks if the game is a draw(in advance).
{
 int d=1,i,j,m,n,o,p;

 for(i=0;i<3;i++)
 {
  m=n=o=p=0;
  for(j=0;j<3;j++)
  {
   m+=tic[i][j];
   n+=tic[j][i];
   o+=tic[j][j];
   p+=tic[j][2-j];
  }

  if(m==1 || m==2 || m==4 || m==8)
   d=0;
  if(n==1 || n==2 || n==4 || n==8)
   d=0;
  if(o==1 || o==2 || o==4 || o==8)
   d=0;
  if(p==1 || p==2 || p==4 || p==8)
   d=0;
 }
 if(d==1)
  winner(0);
}
Exemplo n.º 14
0
//Resolves the battle at planet p, if there is one.
//* Removes all fleets involved in the battle
//* Sets the number of ships and owner of the planet according the outcome
void PlanetState::FightBattle(int myPlanetIndex, const std::vector<Fleet>& fleets, int dt) {
	PlanetState& p = *this;
	std::map<int,int> participants;	
	participants[p.owner] = p.numShips;
	
	for (std::vector<Fleet>::const_iterator f = fleets.begin(); f != fleets.end(); ++f) {
		if (f->turnsRemaining == dt && f->destinationPlanet == myPlanetIndex)
			participants[f->owner] += f->numShips;
	}
	
	Fleet winner(0, 0);
	Fleet second(0, 0);
	for (std::map<int,int>::iterator f = participants.begin(); f != participants.end(); ++f) {
		if (f->second > second.numShips) {
			if(f->second > winner.numShips) {
				second = winner;
				winner = Fleet(f->first, f->second);
			} else {
				second = Fleet(f->first, f->second);
			}
		}
	}
	
	if (winner.numShips > second.numShips) {
		p.numShips = winner.numShips - second.numShips;
		p.owner = winner.owner;
	} else {
		p.numShips = 0;
	}
}
Exemplo n.º 15
0
int main()
{
    ull *N;
    int turn = 1, idx, tests;

    scanf("%d", &tests);

    N = malloc(tests * sizeof (ull));

    for (idx=0; idx < tests; idx++) scanf("%llu", &N[idx]);

    for (idx=0; idx < tests; idx++) {
        ull cntr = N[idx];
        while (cntr>1) {
            if (twoPow(cntr) == 0) cntr = cntr/2;
            else cntr = cntr - lessPow(cntr);
            turn++;
        }
        printf("%s\n", winner(turn));
        turn = 1;
    }

    free (N);
    return 0;
}
Exemplo n.º 16
0
void kurmas_test(CuTest *tc) {
  int num_rows = 8;
  int num_columns = 8;
  int array[num_rows][num_columns];
  int answer;
  ct_initialize(num_rows, num_columns, array);

  placeToken(0, 1, num_rows, num_columns, array);
  placeToken(1, 1, num_rows, num_columns, array);
  placeToken(0, 1, num_rows, num_columns, array);
  placeToken(1, 1, num_rows, num_columns, array);

  placeToken(1, 2, num_rows, num_columns, array);
  placeToken(0, 2, num_rows, num_columns, array);
  placeToken(1, 2, num_rows, num_columns, array);
  placeToken(0, 2, num_rows, num_columns, array);

  placeToken(0, 3, num_rows, num_columns, array);
  placeToken(1, 3, num_rows, num_columns, array);
  placeToken(0, 3, num_rows, num_columns, array);
  placeToken(1, 3, num_rows, num_columns, array);

  placeToken(1, 4, num_rows, num_columns, array);

  answer = winner(num_rows, num_columns, 4, array);

  CuAssertIntEquals_Msg(tc, "Player 1", 1, answer);
}
Exemplo n.º 17
0
void winner_fixes_kurmas_failure(CuTest *tc) {
  int board[8][8];
  ct_initialize(8, 8, board);

  board[7][1] = 0;
  board[6][1] = 1;
  board[5][1] = 0;
  board[4][1] = 1;

  board[7][2] = 1;
  board[6][2] = 0;
  board[5][2] = 1;
  board[4][2] = 0;

  board[7][3] = 0;
  board[6][3] = 1;
  board[5][3] = 0;
  board[4][3] = 1;

  board[7][4] = 1;

  int answer = winner(8, 8, 4, board);
  CuAssertIntEquals_Msg(tc, "Player 1.", 1, answer);

}
Exemplo n.º 18
0
static void make_move(int move)
{
    static XtIntervalId timer = 0;

    // Is it a valid move?
    if ((move < 1) || (move > 9) || (board[move] != NO_ONE))
	return;

    // Are we still thinking?
    if (timer != 0)
	return;

    board[move] = PLAYER1;
    repaint();

    if (winner() == NO_ONE)
    {
	for (int i = 1; i < 10; i++)
	    XtSetSensitive(buttons[i], False);

	// Make a move in THINKING_TIME ms
	timer = XtAppAddTimeOut(XtWidgetToApplicationContext(buttons[move]),
				THINKING_TIME, MoveCB, XtPointer(&timer));
    }
}
Exemplo n.º 19
0
Arquivo: test.cpp Projeto: MaDOS/pi1
///Tests if the winner is calculated correctly
///
///Checks a given field for a winner and tests if the winner is calculated correctly
///Params: field The field to test, winner_code the player who should have won on the given field(0-draw;1-p1;2-p2)
bool test_winner(const int field[SIZE_Y][SIZE_X], const int winner_code)
{
	std::cout << "Running test for 'winner'..." << std::endl;
	std::cout << "----------------------------" << std::endl;
	show_field(field);
	std::cout << "Checking who wins" << std::endl;
	int result = winner(field);
	if (result == winner_code)
	{
		std::cout << "Test passed!" << std::endl;
		if (VERBOSE == 1)
		{
			std::cout << "Calculated result: " << result << std::endl << "Right result: " << winner_code
					  << std::endl;
		}
		return true;
	}
	else
	{
		std::cout << "Test failed!" << std::endl;
		if (VERBOSE == 1)
		{
			std::cout << "Calculated result: " << result << std::endl << "Right result: " << winner_code
					  << std::endl;
		}
		return false;
	}
}
Exemplo n.º 20
0
int main()
{
    int n,player=1,i,j,l;
    scanf("%d",&n);
    for(i=0;i<n;i++)
        scanf("%s",str[i]);
    while(winner(n))
    {
        if(player == 0)
            player = 1;
        else
            player = 0;
        for(i=0;i<n;i++)
        {
           // printf("%s\t",str[i]);
            l=strlen(str[i]);
            for(j=0;j<l;j++)
                str[i][j]=str[i][j+1];
            str[i][j]=0;
        }
    }
    if(player == 0)
        printf("Holmes\n");
    else
        printf("Watson\n");
    return 0;
}
Exemplo n.º 21
0
/*
 * Start the game play. This function is terminated when the board is filled, or
 * when a player has won.
 */
void play()
{
	//X goes first
	char turn = 'X';
	//current number of moves
	int moves = 0;
	
	//draw the board
	initializeGame();
	//go until we find a winner or the board is filled
	while(!winner() && moves < 9) {
		//the current user input
		char c;
		int row, col;
		//wait until we get a valid charter
		while(!validChar(c = getchar()));
		
		//map the character to a row and column
		if(c == 'q' || c == 'w' || c == 'e') {
			//first row
			row = 0;
		} else if(c == 'a' || c == 's' || c == 'd') {
			//second row
			row = 1;
		} else {
			//third row
			row = 2;
		}
		//column check
		if(c == 'q' || c == 'a' || c == 'z') {
			//first col
			col = 0;
		} else if(c == 'w' || c == 's' || c == 'x') {
			//second col
			col = 1;
		} else {
			//third col
			col = 2;
		}
		
		//check if that space is free, otherwise loop goes again
		if (grid[row][col] == '\0') {
			//mark that spot
			grid[row][col] = turn;
			//draw it an change turns
			if (turn == 'X') {
				drawX(row,col);
				turn = 'O';
			} else if (turn == 'O') {
				drawO(row,col);
				turn = 'X';
			}
			//add the the move count
			moves++;
		}
	}
	//after the game is over, wait for the user to press any key to end
	getchar();
}
Exemplo n.º 22
0
int
main () {
	int matrix[3][3] = { 0, 0, 1,
			     1, 1, 1,
			     1, 1, 0};
	printf ("\n Winner=%d\n", winner (matrix));
	return 0;
}
Exemplo n.º 23
0
void SGBattle::SendWinner()
{
	SFProtobufPacket<SevenGamePacket::Winner> winner(SevenGame::Winner);

	winner.GetData().set_playerindex(*(m_pSevenGameManager->m_vecWinner.begin()));

	BroadCast(winner);
}
Exemplo n.º 24
0
void print_winner(struct board_t board) {
	system("clear");
	if(winner(board) == 1) {
		printf("%s won the game!\n", board.pl[0].name);
	} else {
		printf("%s won the game!\n", board.pl[1].name);
	}
}
Exemplo n.º 25
0
void win()
{ int i;
if(digits[0]*digits[4]*digits[8]==1 || digits[6]*digits[4]*digits[2]==1 || digits[0]*digits[4]*digits[8]==8 || digits[6]*digits[4]*digits[2]==8 )
{
    winner();
}
for(i=0;i<9;i+=3)
{
 if( digits[i]*digits[i+1]*digits[i+2] == 1 ||  digits[i]*digits[i+1]*digits[i+2] == 8)
    {winner();}
}
for(i=6;i<9;i++)
{
  if( digits[i]*digits[i-3]*digits[i-6] == 1 || digits[i]*digits[i-3]*digits[i-6] == 8)
    {winner();}
}
}
Exemplo n.º 26
0
void gameplay()
{
    int i=0;
    char c=0;
    char choice=0;

    printf("Wpisz T aby dobrac karte.\n");
    printf("Wpisz Q aby zakonczyc.\n");

    while(c=getchar())
    {
        if(i==0)
        {
            if(c=='T'||c=='t'||c=='Q'||c=='q') choice = c;
            else choice = 0;
            i++;
        }
        else
        {
            if(choice&&i==1&&c=='\n')
                switch(choice)
                {
                case 't': {
                    take();
                    show_table(d,p);
                    printf("Wpisz T aby dobrac karte.\n");
                    printf("Wpisz Q aby zakonczyc.\n");
                    i=0;
                    break;
                }
                case 'T': {
                    take();
                    show_table(d,p);
                    printf("Wpisz T aby dobrac karte.\n");
                    printf("Wpisz Q aby zakonczyc.\n");
                    i=0;
                    break;
                }
                case 'q':
                    break;
                case 'Q':
                    break;
                }/*switch*/
            else if (c=='\n') {
                printf("Niewlasciwa komenda! Mozesz dobrac karte (T) lub zakonczyc ture (Q)\n");
                i=0;
            }
            else i++;

        }/*else*/
        if((choice=='Q'||choice=='q')&&c=='\n'&&i==1) {
            /*show_table(d,p);*/break;
        }
    }

    winner();
}/*gameplay()*/
Exemplo n.º 27
0
void player_vs_player(void) {
    char x_name[STRING_LENGTH];
    char o_name[STRING_LENGTH];
    board_type board;
    int move_num = 1;
    char player = 'X';
    
    system("cls");
    memset(x_name, '\0', STRING_LENGTH);
    memset(o_name, '\0', STRING_LENGTH);
    
    init(board);
    
    printf("Enter the name of player 'X': ");
    fflush(stdin);
    scanf("%32s", &x_name);
    printf("Enter the name of player 'O': ");
    fflush(stdin);
    scanf("%32s", &o_name);
    
    while(winner(board) == empty) {
        system("cls");
        printf("Match: Player vs. Player\n\n");
        print_board(board);
        if(player == 'X')
            printf("It is turn for %s!\n", x_name);
        else
            printf("It is turn for %s!\n", o_name);
        
        move(board, player, move_num, true, '\0');
        player = other_player(player);
        move_num++;
    }
    system("cls");
    printf("Match: Player vs. Player\n\n");
    print_board(board);
    
    if(winner(board) != 'T')
        printf("Winner is %s!\n", winner(board) == 'X' ? x_name : o_name);
    else
        printf("Draw!\n");
    
}
Exemplo n.º 28
0
void game(int whoClicked){ //determine who gained points
	if(isCountdownFinished==1){	
		++user[whoClicked];
	}
	else{
			++user[!whoClicked];
	}	
	isGameEnded = 1;
	winner(user);
}
Exemplo n.º 29
0
void test_board_finds_winner() {
  Board* board = new_board(3);
  make_move(board, 2, 'X');
  make_move(board, 1, 'O');
  make_move(board, 4, 'X');
  make_move(board, 0, 'O');
  make_move(board, 6, 'X');
  assert(winner(board) == 'X');
  destroy_board(board);
}
Exemplo n.º 30
0
void Controller::collisions(Warrior *war)
{
	if((timmy->getX()==king->getX())&&(timmy->getY()==king->getY())){
		winner();
		
	}
	else if((harold->getX()==king->getX())&&(harold->getY()==king->getY())){
		winner();
		
	}
	else{
		Node<Warrior> *temp = WarriorList->getFront();
		while(temp!=0)
		{
			if(temp->getData()!=war)//making sure it doesnt collide with itself
			{
				if((temp->getData()->getX()==war->getX())&&
				temp->getData()->getY()==war->getY()){
					//check for 2 soldiers
					Soldier *test1 = dynamic_cast<Soldier*>(war);
					Soldier *test2 = dynamic_cast<Soldier*>(temp->getData());
					if(test1!=0&&test2!=0)
					{
						//soldier battle
						WarriorList->pop(war);
						WarriorList->pop(temp->getData());
					}
					else if(test1!=0&&test2==0)//one soldier and 1 warrior
					{
						temp->getData()->damage(war->getStrength());
					}
					else if(test1==0&&test2!=0)
					{
						war->damage(temp->getData()->getStrength());
					}
				}
			}

			temp = temp->getNext();
		}
	}	
}