Example #1
0
bool Game::startGame(int size){
	this->length= size;
	newBoard(this->length);
	bool gameEnd = false;
	bool victory = false;
	do
	{
		insertNewNumber();
		if (checkIfGameOver() == true){
			break;
		}
		string oldNumber = uniqNumber();

		string message = "Press up, down, left, right or q for quit";
		while (1){
			PrintBoard();
			string d = input(message);
			if (d == "quit"){
				gameEnd=true;
				break;
			}
			moveDirection(d);	//left worked
			if (oldNumber != uniqNumber()){
				break;
			}
			message = "You can't go there, try another button or q for quit";
		}
		if (victory==false){
			victory = checkIfVictorius();
		}
	}
	while(gameEnd==false);
	PrintBoard();
	return victory; 
}
Example #2
0
int main(void){
    //initialize stuff
    init_zobrist();
    srand(time(NULL));
    //create a new board
    Board_t* goal = newBoard();
    //create a new visited cache
    HashTable_t* visited = newHashTable(32);
    //create a cloned state of the board and scrable it
    Board_t* scrabled = cloneBoard(goal);
    scramble_times(scrabled, TIMES_TO_SCRAMBLE);

    printf("Starting search...\n");
    SearchNode_t* root = newSearchNode(scrabled, 0, NULL);
    SearchNode_t* result = idf_search(root, goal, visited, 30);

    //print out the search
    if(result!=NULL){
        printf("Search Path:\n");
        printSearchNode(result);
    }
    else
        printf("Search failed.\n");

    //clean up
    deleteSearchNode(root, visited);
    assert(TOTAL_SEARCH_NODES==0);
    assert(TOTAL_HASH_NODES==0);
    deleteHashTable(visited);
    deleteBoard(goal);
    return 0;
}
int UltimateTicTacToeMontecarloAI::expand(int leafIndex, Nodes& nodes, int const player) const
{
  Node& node = nodes[leafIndex];
  node.children.reserve(maxChildren);
  Moves options = movementOptions(node.board, node.previousMove);
  int turn = node.previousMove > 0 ? otherPlayer(node.board.grids.at(node.previousMove)) : player;

  int mostPromisingChildIndex = -1;
  int mostPromisingChildScore = 0;

  while(node.children.size() < maxChildren && !options.empty())
  {
    Move move = options.takeAt(qrand() % options.size());
    int childIndex = nodes.size();
    node.children.append(childIndex);
    Board newBoard(node.board);
    nodes.append( Node {0, 1, playMove(newBoard, move, turn), move, leafIndex, Node::Children()});
    int score = scoreBoard(nodes.last().board, player);
    if(score > mostPromisingChildScore || mostPromisingChildIndex < 0)
    {
      mostPromisingChildIndex = childIndex;
      mostPromisingChildScore = score;
    }
  }

  return mostPromisingChildIndex;
}
Example #4
0
int main() {
	char playAgain;

	do {
		nodes_expanded = 0;
		//initialize the board.
		char *board = newBoard();

		//get player and computer characters.
		char playerPiece = askUser("\nPlease choose your character (x or o): ", X, O);
		char computerPiece = getOpponentPiece(playerPiece);

		//check if player wants to play first.
		char ch = askUser("\nDo you want to play first ? (y or n): ", 'y', 'n');
		if(ch == 'y') {
			makePlayerMove(board, playerPiece);			
		}
		else if(ch == 'n') {
		}

		char winner;
		int turn = COMPUTER;

		//game loop.
		while(!isFilled(board) && (winner = whoWon(board)) == NONE) {

			if(turn == COMPUTER) {
				makeComputerMove(board, computerPiece);
				turn = PLAYER;
			}
			else {
				makePlayerMove(board, playerPiece);
				turn = COMPUTER;
			}
		}

		printBoard(board);

		//print results.
		if(winner == playerPiece) {
			printf("\n$ Congratulations! you have won. $\n");
		}
		else if(winner == NONE) {
			printf("\n* The game is a draw. *\n");
		}
		else {
			printf("\n@ You Lose!! @\n");
		}

		printf("\nNumber of nodes expanded: %d", nodes_expanded);

		//ask if user wants to play again.
		playAgain = askUser("\n Do you want to play again? (y or n): ", 'y', 'n');
	}while(playAgain == 'y');

	return 0;
}
Example #5
0
int main(void)
{
    
    char ** board = newBoard();
    
    // print top row of numbers
    PickPiece(board);
   // return contents;
    
    displayBoard(board);

}
Example #6
0
/*
* @method
*   Board Board::swap(Position slot, Position token)
*     - returns a new board with slot and token positions switched
*   Author: Mark Sands
*   Date modified: 2-3-11
*/
Board Board::swap(const Position& slot, const Position& token) const
{
    Board newBoard(*this);

    std::swap(newBoard.board[size_*slot.col + slot.row], newBoard.board[size_*token.col + token.row]);
    std::swap(newBoard.chargrid[size_*slot.col + slot.row], newBoard.chargrid[size_*token.col + token.row]);

    // update the emptySlotIndex, if that was a swapped tile
    if (newBoard.emptySlotIndex == slot)
        newBoard.emptySlotIndex = token;

    return newBoard;
}
Example #7
0
void Editor::topToolbarOption(int option) {

    switch(option) {
	case ID_TOOL_NEW:
		newBoard();
		break;
	case ID_TOOL_LOAD:
		loadBoard();
		break;
	case ID_TOOL_SAVE:
		saveBoard();
		break;
	case ID_TOOL_LEFT:
		theBoard.shiftLeft();
		repaint(false);
	        break;
	case ID_TOOL_RIGHT:
		theBoard.shiftRight();
		repaint(false);
	        break;
	case ID_TOOL_UP:
		theBoard.shiftUp();
		repaint(false);
	        break;
	case ID_TOOL_DOWN:
		theBoard.shiftDown();
		repaint(false);
	        break;
	case ID_TOOL_DEL:
			mode=remove;
		break;

	case ID_TOOL_MOVE:
			mode=move;
		break;

	case ID_TOOL_ADD:
			mode = insert;
		break;
	case ID_META_EXIT:
			close();
		break;

	default:

	break;
    }

}
Example #8
0
void sudokuBoard::newBoard() {

  // Robust generator
  populateBoard();
  obfuscateBoard();
  if ( isSolvable() ) {
    isCreated = true;
    isSolved = false;
  } 
  else 
    newBoard();

  
  
}
Example #9
0
void KardView::slotAll()
{
    Kard::playSound("kard_gameover.wav");
    int bestNum = n*m/2;
    QString mString = i18n("Congratulations!\n"
    "You finished the game in %1 tries.\n"
    "The best you could have done is %2 tries.\n"
    "Do you want to play again?",  tries, bestNum);
    switch(KMessageBox::questionYesNo( this, mString,i18n("Game is Finished") ))  {
    case 3: // yes
	//put an icon for yes (animal nodding) and no
	newBoard();
	break;
    case 4:	// no
	kapp->quit();
	break;
    }
}
Example #10
0
File: 4xl.c Project: tabaha/4xl
int main()
{
	int gtype;
	board *gboard = newBoard(' ');
	welcomeMessage(gboard);
	gtype = chooseOpp();
	if(gtype == 0) 
	{
		while(1)
		{
			playWithCPU(gboard);
			printf("\nNEW GAME\n");
			gboard->round = 0;
			gboard->timesPlayed++;
			emptyBoard(gboard);
		}
	}
	return 0;
}
Example #11
0
int sudokuBoard::actions() {
  char optBuffer[80];
  int option = 0;
  cout<<"\n\tAction: ";
  cin >> optBuffer;
  if ( !isdigit(optBuffer[0]) ) {
    cout << "\n\t\tNot a number ==>  Please try again" << endl;
    actions();
  }
  else 
    option = atoi (optBuffer);


  switch(option) {
    case(1):
        cout << endl;
        if ( !update() )    
            return EXIT_FAILURE;
        cout << "\033[1;32mAdding Update\033[0m"<<endl;
        sleep(2);
        break;
    case(2): 
        cout << "\n\033[1;32mChecking solution\033[0m"<<endl;
        sleep(2);
        break;
    case(3):
        cout << "\n\033[1;32mNew puzzle\033[0m"<<endl;
        newBoard();
        sleep(2);
        break;
    case(4):
        cout << "\n\033[1;32mExiting... \033[0m\n" << endl;
        sleep(1);
        return EXIT_END; // ( Successful finish of the program)
    default: 
        cout << "\n\033[1;31mIncorrect option entered! Try again\033[0m\n" << endl;
        return actions();
        break;
  }
  return true;
}
Example #12
0
int main() {
	Board board = newBoard();
	Player player = newPlayer(1, PLAYER, board),
		   current;
	AI ai = newAI(board);
	WINDOW *status;
	bool isRunning = TRUE;
	int key;

	initscr();				/* Start curses mode */
	start_color();			/* Color mode */
	cbreak();				/* Disable line-break mode */
	keypad(stdscr, TRUE);	/* Enable getch for function keys */
	curs_set(0);			/* Remove cursor */
	noecho();				/* Disable echo */
	init_pair(1, COLOR_YELLOW, COLOR_BLACK);
	init_pair(2, COLOR_RED, COLOR_BLACK);
	init_pair(3, COLOR_YELLOW, COLOR_YELLOW);
	init_pair(4, COLOR_RED, COLOR_RED);

	status = newwin(25, 20, 0, 0);
	current = player;

	boardDraw(board);
	statusDraw(status, current, ai);
	ungetch('a');	/* Skip first getch() */

	while(isRunning) {
		if((key = getch()) == KEY_F(1)) {
			isRunning = FALSE;
			break;
		}
		
		if(current == player) {
			if(player->doMove(player, key)) {
				/* Pass turn to AI */
				current = ai->player;
				ungetch('a'); /* Skip the next keystroke and instantly go to the AI */
			}
		} else {
			ai->doAIMove(ai);
			if(ai->player->doMove(ai->player, key)) {
				/* Pass turn to AI */
				current = player;
				ungetch('a'); /* Skip the next keystroke and instantly go to the AI */
			}
		}

		boardDraw(board);
		statusDraw(status, current, ai);

		if(board->isBoardFull(board)) {
			getch();
			wmove(status, 13, 1); wclrtoeol(status);
			wattron(status, A_BOLD);
			wprintw(status, "Draw Game!");
			wattroff(status, A_BOLD);
			wrefresh(status);

			isRunning = FALSE;
			getch();
		}
		if(board->checkForWin(board, current->getID(current))) {
			int pid = current->getID(current);

			getch();
			wmove(status, 13, 1); wclrtoeol(status);
			wattron(status, A_BOLD | COLOR_PAIR(pid));
			wprintw(status, "%s", (pid == 1)?"Player":"AI");
			wattroff(status, A_BOLD | COLOR_PAIR(pid));
			wprintw(status, " won!");
			wrefresh(status);

			isRunning = FALSE;
			getch();
		}
	}

	board->destroyBoard(board);
	free(player);
	free(ai);
	endwin();
	return 0;
}