Esempio n. 1
0
void CTraceZler::OnMenuLast()
{
	memcpy(board, trcboard, sizeof(board));
	m_nIdxVisist--;
	//更新盘面
	UpdateBoard(&TrList1[TrList2[m_nIdxVisist].index]);
	UpdateBoard(&TrList2[m_nIdxVisist]);

	SendViewInd_BrowseState();
	SendMenuInd_BrowseState();
}
Esempio n. 2
0
int PlayGame(int *Parameter){

    Board gameBoard,nextBoard;
    int i;
    int maxLevel;

    InitGame(gameBoard);
    PrintBoard(gameBoard);

    while (1){
        NextStep(nextBoard,gameBoard, Parameter, COUNT_STEP, MoveLeft, MoveRight, MoveUp, MoveDown);
        UpdateBoard(gameBoard,nextBoard);
        maxLevel = FinishGame(gameBoard);
        if ( maxLevel != 0){
            break;
        }
        if (PLAY_MODE == 1)
            if ( TotalScore > 1000)
                Sleep(1000);
            else
                Sleep(100);
    }

    return maxLevel;
}
int CPlayer::processMoveMessage(tictacpacket *pPacket)
{
    cout << "I am inside Function " << __func__ << endl;

    UpdateBoard(*pPacket);

    return 0;
}
Esempio n. 4
0
/* This function is called when a human player clicks on a square with the mouse */
void SquareChosen(struct Square *sq)
{
    int result;

    if(playing && player[turn] == HUMAN) {
        if(hlen == 0) {    /* This is the first square chosen */
            if(sq->state && sq->col == turn) {
                hmove[hlen] = sq->val;
                hlen++;
                sq->hilite = turn;
                UpdateBoard();
            }
        } 
        else {    /* This is NOT the first square chosen */
            if(!sq->state) {
                /* Add square to move list and check to see if the current move */
                /* is illegal, partially complete, or a completed legal move */
                hmove[hlen] = sq->val;
                hlen++;
                result = CheckHumanMove();
                if(result == ILLEGAL) {
                    hlen = 0;
                    UnHighlightAll();
                    memset(hmove,0,12*sizeof(int));
                }
                else if(result == PARTIAL) {
                    sq->hilite = turn;
                    UpdateBoard();
                }
                else if(result == FULL) {
                    UnHighlightAll();
                    HumanMoved = 1;
                }    
            } 
            else {
                hlen = 0;
                UnHighlightAll();
                memset(hmove,0,12*sizeof(int));
            }
        }    
    }
}
Esempio n. 5
0
/* squares he/she has chosen */
void UnHighlightAll(void)
{
    int x,y;

        for(y=0; y<8; y++)
        for(x=0; x<8; x++)
        if(x%2 != y%2) {
        square[y][x].hilite = Green;
        }
    UpdateBoard();
}
Esempio n. 6
0
// Process a key press
// left/right moves through history; 'p' passes the turn
void SimpleGoPanel::KeyDown(wxKeyEvent& event)
{	if(event.GetKeyCode()==WXK_LEFT&&curmove>0)
	{	gnugopause = true;
		gnugoscore = false;
		curmove--;
		UpdateBoard();
	}
	else if(event.GetKeyCode()==WXK_RIGHT&&curmove<totmove)
	{	gnugoscore = false;
		curmove++;
		UpdateBoard();
	}
	else if(event.GetKeyCode()=='P'&&!event.AltDown())
		MakePass();
	else if(event.GetKeyCode()==WXK_ESCAPE)
	{	gnugopause = true;
		frame->playmenu->Check(ID_RANDOM, false);
	}
	event.Skip();
}
Esempio n. 7
0
void *DataSender(void *arg) {
  std::vector<Client *> client_out;
  
  while (true) {
    UpdateBoard();
    client_out.clear();
    GetClients(&client_out);
    SendData(&client_out);
    usleep(100000);
  }
}
Esempio n. 8
0
void CTraceZler::OutputResult()
{
	SaveToFile();
	//输出结果后执行
	if (TrCount2 == 0)
	{
		SendViewInd_BrowseState();
		SendMenuInd_BrowseState();
	}
	else
	{
		memcpy(board, trcboard, sizeof(board));
		//更新盘面
		UpdateBoard(&TrList1[TrList2[0].index]);
		UpdateBoard(&TrList2[0]);

		SendViewInd_BrowseState();
		SendMenuInd_BrowseState();
	}
}
Esempio n. 9
0
void CTraceZler::SaveToFile()
{
	TCHAR wcsTraceFolder[_MAX_PATH];
	wsprintf(wcsTraceFolder, L"%s\\Trace", ModulePath);
	//进入Trace目录
	if (_wchdir(wcsTraceFolder) != 0)
	{
		_wmkdir(wcsTraceFolder);
		_wchdir(wcsTraceFolder);
	}
	//写入序号文件
	static TCHAR wcsFileName[_MAX_PATH];
	for (int i = 0; i < TrCount2; i++)
	{
		UpdateBoard(&TrList1[TrList2[i].index]);
		UpdateBoard(&TrList2[i]);
		wsprintf(wcsFileName, L"%d_%d.pgn", m_nTraceTimes, i);
		SaveToPgn(wcsFileName);
		//还原盘面
		RestoreBoard(&TrList2[i]);
		RestoreBoard(&TrList1[TrList2[i].index]);
	}
}
Esempio n. 10
0
void *StartWorker(void *w)
{
	while(1)
	{
		Task* t = PullTask(((Worker *)w)->tq);
		if(t->id != ENDTASK)
		{
			UpdateBoard(((Worker *)w)->rb,t->id,t->fun(t->arg));
			free(t);
		}
		else
		{
			pthread_exit(NULL);
			free(t);
		}
	}
}
Esempio n. 11
0
void SnakeGame::UpdateBoardState()
{
	bool gameover = false;

	int p1Next = snakeBody[0].front() + playerMov[0];
	snakeBody[0].insert(snakeBody[0].begin(), p1Next);

	int p2Next = snakeBody[1].front() + playerMov[1];
	snakeBody[1].insert(snakeBody[1].begin(), p2Next);

	if (CheckHitBoundary(snakeBody[0].front(), playerMov[0])) {
		playerScore[1]++;
		gameover = true;
	}
	if (CheckHitBoundary(snakeBody[1].front(), playerMov[1])) {
		playerScore[0]++;
		gameover = true;
	}

	if (!gameover) {
		if (fruitPos != snakeBody[0].front())
			snakeBody[0].pop_back();
		else {
			RandomSpawnFood();
			playerScore[0]++;
		}

		if (fruitPos != snakeBody[1].front())
			snakeBody[1].pop_back();
		else {
			RandomSpawnFood();
			playerScore[1]++;
		}
	}

	ClearBoard();
	if (gameover || !UpdateBoard())
		ResetBoardState();
}
Esempio n. 12
0
int main(int argc, char **argv) {

	std::string board = std::string(BOARD_WIDTH*BOARD_HEIGHT, '=');

	int p1Move = 1;
	int p2Move = -1;
	int p1Score = 0;
	int p2Score = 0;
	int fruitPos = 0;

	std::vector<int> player1;
	std::vector<int> player2;

	ResetBoard(board, player1, player2, fruitPos, p1Move, p2Move);


	bool quit = false;

	while (!quit) {
		//REMOVE FOR FINAL CODE
		system("cls");

		//REMOVE FOR FINAL CODE
		//Change to receive client comamnds
		if (_kbhit())
		{
			switch (_getch())
			{
			//Player 1 Movement
			case 'a': 
				if(p1Move != 1)
					p1Move = -1;
				break;
			case 'd':
				if(p1Move != -1)
					p1Move = 1;
				break;
			case 'w': 
				if(p1Move != BOARD_WIDTH)
					p1Move = BOARD_WIDTH*-1;
				break;
			case 's': 
				if(p1Move != BOARD_WIDTH*-1)
					p1Move = BOARD_WIDTH*1;
				break;

			//Player 2 Movement
			case 75:
				if(p2Move != 1)
					p2Move = -1;
				break;
			case 77:
				if(p2Move != -1)
					p2Move = 1;
				break;
			case 72:
				if(p2Move != BOARD_WIDTH)
					p2Move = BOARD_WIDTH*-1;
				break;
			case 80: 
				if(p2Move != BOARD_WIDTH*-1)
				p2Move = BOARD_WIDTH * 1;
				break;

			}
		}

		bool gameover = false;

		int p1Next = player1.front() + p1Move;
		player1.insert(player1.begin(),p1Next);

		int p2Next = player2.front() + p2Move;
		player2.insert(player2.begin(), p2Next);

		if (HitBoundary(player1.front(), p1Move)) {
			p2Score++;
			gameover = true;
		}
		if (HitBoundary(player2.front(), p2Move)) {
			p1Score++;
			gameover = true;
		}

		if (!gameover) {
			if (fruitPos != player1.front())
				player1.pop_back();
			else {
				RandomSpawn(fruitPos, board);
				p1Score++;
			}

			if (fruitPos != player2.front())
				player2.pop_back();
			else {
				RandomSpawn(fruitPos, board);
				p2Score++;
			}
		}

		ClearBoard(board);
		if(gameover || !UpdateBoard(board, player1,player2,fruitPos,p1Score,p2Score,p1Move,p2Move))
			ResetBoard(board, player1, player2, fruitPos, p1Move, p2Move);


		//REMOVE FOR FINAL CODE
		DisplayState(board,p1Score,p2Score);
		Sleep(100);
	}


	
	return 0;
}
Esempio n. 13
0
int main(int argc, char *argv[])
{
    char text[1028],temptext[1028], str[1028];
    int tlen,mlen,move[12],numlegal,done;
    if(argc>=3)
    {
       if(!strncasecmp("-MaxDepth",argv[argc-2], strlen("-MaxDepth")))
       {
          MaxDepth = atoi(argv[argc-1]);
          argc-=2;
       }
       else MaxDepth = -1;
    }
    else MaxDepth = -1;

#ifndef GRAPHICS
    printf("No graphics\n");
    if(argc != 4) Usage(argv[0]);
    strcpy(player1,argv[1]);
    strcpy(player2,argv[2]);
    SecPerMove = atof(argv[3]);
#endif

#ifdef GRAPHICS
    printf("Graphics\n");
    InitGraphics(argc,argv);
#else
      NewGame();
      {
     int x,y;
     /* I'll wait a bit to make sure both oponents are ready to go */
     printf("waiting\n");
     sleep(1);
     for(x=0;x<1000;x++)
     for(y=0;y<10000;y++);
      }
#endif
    ResetBoard();
    for(;;) {
        pthread_t thread;
        int rc, dummy;
        HandleEvents();
        if(playing) {
            sprintf(str,"Waiting for player %d",turn+1);
            Message(str);
            HumanMoved = done = 0;
            //start = times(&bff);
            rc = pthread_create(&thread, NULL, timer, (void*)&done);
            pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, &dummy);
            do {
                HandleEvents();
                /* Give humans all the time they want to move */
                if(player[turn] == HUMAN) done = HumanMoved;
                else if(player[turn] == COMPUTER) 
                {
                    char *ptr;
                    memset(temptext,0,sizeof(temptext));
                    tlen = read(readfd[turn],temptext,1028);
                    if(tlen > 0) 
                    {
                        ptr = temptext;
                        while(*ptr == 10 && *ptr !=0) ptr++;
                        strcpy(text,ptr);
                        if(strlen(text)) done=1;
                    }
                }
            } while(playing && !done);
            pthread_cancel(thread);
            if(!playing) continue;
            if(player[turn] == COMPUTER && tlen <= 0) {
                sprintf(str,"Player %d has lost the game (time limit reached).",turn+1);
                Message(str);
                StopGame();
            }    
            else {
                if(player[turn] == COMPUTER) {
                    text[tlen] = '\0';
                    memset(move,0,12*sizeof(int));
                    mlen = TextToMove(text,move);
                }
                else if(player[turn] == HUMAN) {
                    mlen = hlen;
                    memcpy(move,hmove,12*sizeof(int));
                    hlen = 0;
                    memset(hmove,0,12*sizeof(int));
                    MoveToText(move,text);
                }

                if(!mlen) { /* Illegal move check 1 */
                    /*char temp[1000];
                    char *ptr1, *ptr2;
                    ptr1=text;
                    temp[0] = 0;
                    ptr2=temp;
                    while(*ptr1) 
                    {
                        sprintf(ptr2,"%i, ", *ptr1);
                        ptr1++;
                        ptr2 = &(ptr2[strlen(ptr2)]);
                    }*/
                    //sprintf(str,"Player %d has lost the game (illegal move %s %s submitted).",turn+1,text, temp);
                    sprintf(str,"Player %d has lost the game (illegal move %s submitted).",turn+1,text);
                    Message(str);
                    StopGame();
                }
                else {
                    if(!IsLegal(move,mlen)) { /* Illegal move check 2 */
                       /*char temp[1000];
                       char *ptr1, *ptr2;
                       ptr1=text;
                       temp[0] = 0;
                       ptr2=temp;
                       while(*ptr1) 
                       {
                           sprintf(ptr2,"%i, ", *ptr1);
                           ptr1++;
                           ptr2 = &(ptr2[strlen(ptr2)]);
                       }
                        sprintf(str,"Player %d has lost the game (illegal move %s %s submitted).",turn+1,text, temp);*/
                        sprintf(str,"Player %d has lost the game (illegal move %s submitted).",turn+1,text);
                        Message(str);
                        StopGame();
                    }
                    else {  /* Legal Move */
                        PerformMove(move,mlen);
#ifdef GRAPHICS
                        UpdateBoard();
#else
                        printf("Move: %s\n",text);
                        PrintBoard();
#endif
                        if(turn) turn=0; else turn=1;
    
                        /* Check to see if other player has now lost */
                        numlegal = FindLegalMoves(turn+1);
                        if(!numlegal) {
                            sprintf(str,"Player %d has lost the game.",turn+1);
                            Message(str);
                            StopGame();
                        }
                        else if(player[turn] == COMPUTER) {
                            write(writefd[turn],text,strlen(text));
                        }
                    }
                }
            }
        }            
    }
}