// TicTacToeGame.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include <iostream> #include <conio.h> #include <iomanip> #include <time.h> void FunctionDrawMenu(void); void FunctionDrawField(char *cArray, int iArraySizeRow, int iArraySizeColum); void FunctionViewField(char *cArray, int iArraySizeRow, int iArraySizeColum); void FunctionChangesField(char *cArray, int iArraySizeRow, int iArraySizeColum, char cCaseChanges, char cXOrToe); void FunctionViewBitMap(int *cArray, int iArraySizeRow, int iArraySizeColum); bool FunctionBitMapField(int *cArray, int iArraySizeRow, int iArraySizeColum, bool bZeroing, int iCellNumber, int iXOrToe); int FunctionAI(int *iArrBitMap, int iArraySizeRow, int iArraySizeColum, int iUserFigure, int iPcSymbolTurn); int FunctionConversionTurnPC(int iKayConversion); int FunctionCheckWinner(int *iArrBitMap, int iArraySizeRow, int iArraySizeColum, int iUserSymbolTurn, int iPcSymbolTurn); int _tmain(int argc, _TCHAR* argv[]) { const int iSIZE_ROW_FIELD = 7; const int iSIZE_COLUM_FIELD = 7; const int iSIZE_ROW_FIELD_BITMAP = 3; const int iSIZE_COLUM_FIELD_BITMAP = 3; int iChoiceGameKay, iNumberOfGames; char cMapField[iSIZE_ROW_FIELD][iSIZE_COLUM_FIELD]; //An array of imaging the playing field int iBitMapField[iSIZE_ROW_FIELD_BITMAP][iSIZE_COLUM_FIELD_BITMAP] = {0}; //An array of field map, to calculate the moves of players FunctionDrawMenu(); iChoiceGameKay = _getch(); /* *The choice of gameplay modes */ switch(iChoiceGameKay) { case 97: // Choice "A" std::cout<<"Please enter the number of games "; std::cin>>iNumberOfGames; for(;iNumberOfGames > 20;system("cls")) { std::cout<<"In this mode, you can not play more than 20 games"<<std::endl; std::cout<<"Please enter the number of games again "; std::cin>>iNumberOfGames; } break; case 98:// Choise "B" iNumberOfGames = 100; break; } /* *The cycle of games */ int iUserFigure; // 1-"x" 2-"0" int iPCFigure; // 1-"x" 2-"0" char cFigureUser, cFigurePC; bool bGoFirst; int iTurnKay, iPcKay, ConversionPcKay; //Key Variables for(int bWin = 0, iStatisticsWinPC = 0, iStatisticsWinUser = 0, iStatisticsNOwinner = 0; iNumberOfGames ; --iNumberOfGames, bWin = 0, system("cls")) { /* *Initialize an array of visualizations and an array of logical calculations. */ FunctionDrawField(&cMapField[0][0], iSIZE_ROW_FIELD, iSIZE_COLUM_FIELD); FunctionBitMapField(&iBitMapField[0][0], iSIZE_ROW_FIELD_BITMAP, iSIZE_COLUM_FIELD_BITMAP, true, 1, 1); std::cout<<"Select one to play: "<<std::endl; std::cout<<std::endl; std::cout<<"a) \"X\" - Go first "<<std::endl; std::cout<<std::endl; std::cout<<"b) \"O\" - Go past"<<std::endl; std::cout<<std::endl; std::cout<<"Enter your choice "; std::cout<<std::endl; /* *Checking, whether a selection is made */ for(int iKayChoise = 0;;) { iKayChoise = _getch(); switch(iKayChoise) { case 97: //Choise A iUserFigure = 5; iPCFigure = 7; break; case 98: //Choise B iUserFigure = 7; iPCFigure = 5; break; } if(iKayChoise == 97 || iKayChoise == 98) { break; } } /* *Setting the player characters, depending on the choice of */ if(iUserFigure == 5) { cFigureUser = 'X'; cFigurePC = 'O'; } else { bGoFirst = false; cFigureUser = 'O'; cFigurePC = 'X'; } /* *Game loop */ iPcKay = 0; ConversionPcKay = 0; //////////////////////////////////////////////////////////////// for(;;system("cls"), bGoFirst = true) { system("cls"); FunctionViewField(&cMapField[0][0], iSIZE_ROW_FIELD, iSIZE_COLUM_FIELD); //FunctionViewBitMap(&iBitMapField[0][0], iSIZE_ROW_FIELD_BITMAP, iSIZE_COLUM_FIELD_BITMAP); /* *Check who win */ if(bWin == 1 || bWin == 2 || bWin == 3) { system("cls"); break; } /* */ /* *Processing of the user */ if(bGoFirst)//Checking who first walks { iTurnKay = _getch(); switch(iTurnKay) { case 49: if(FunctionBitMapField(&iBitMapField[0][0], iSIZE_ROW_FIELD_BITMAP, iSIZE_COLUM_FIELD_BITMAP, false, 1, iUserFigure)) { FunctionChangesField(&cMapField[0][0], iSIZE_ROW_FIELD, iSIZE_COLUM_FIELD, iTurnKay, cFigureUser); } else { continue; } break; case 50: if(FunctionBitMapField(&iBitMapField[0][0], iSIZE_ROW_FIELD_BITMAP, iSIZE_COLUM_FIELD_BITMAP, false, 2, iUserFigure)) { FunctionChangesField(&cMapField[0][0], iSIZE_ROW_FIELD, iSIZE_COLUM_FIELD, iTurnKay, cFigureUser); } else { continue; } break; case 51: if(FunctionBitMapField(&iBitMapField[0][0], iSIZE_ROW_FIELD_BITMAP, iSIZE_COLUM_FIELD_BITMAP, false, 3, iUserFigure)) { FunctionChangesField(&cMapField[0][0], iSIZE_ROW_FIELD, iSIZE_COLUM_FIELD, iTurnKay, cFigureUser); } else { continue; } break; case 52: if(FunctionBitMapField(&iBitMapField[0][0], iSIZE_ROW_FIELD_BITMAP, iSIZE_COLUM_FIELD_BITMAP, false, 4, iUserFigure)) { FunctionChangesField(&cMapField[0][0], iSIZE_ROW_FIELD, iSIZE_COLUM_FIELD, iTurnKay, cFigureUser); } else { continue; } break; case 53: if(FunctionBitMapField(&iBitMapField[0][0], iSIZE_ROW_FIELD_BITMAP, iSIZE_COLUM_FIELD_BITMAP, false, 5, iUserFigure)) { FunctionChangesField(&cMapField[0][0], iSIZE_ROW_FIELD, iSIZE_COLUM_FIELD, iTurnKay, cFigureUser); } else { continue; } break; case 54: if(FunctionBitMapField(&iBitMapField[0][0], iSIZE_ROW_FIELD_BITMAP, iSIZE_COLUM_FIELD_BITMAP, false, 6, iUserFigure)) { FunctionChangesField(&cMapField[0][0], iSIZE_ROW_FIELD, iSIZE_COLUM_FIELD, iTurnKay, cFigureUser); } else { continue; } break; case 55: if(FunctionBitMapField(&iBitMapField[0][0], iSIZE_ROW_FIELD_BITMAP, iSIZE_COLUM_FIELD_BITMAP, false, 7, iUserFigure)) { FunctionChangesField(&cMapField[0][0], iSIZE_ROW_FIELD, iSIZE_COLUM_FIELD, iTurnKay, cFigureUser); } else { continue; } break; case 56: if(FunctionBitMapField(&iBitMapField[0][0], iSIZE_ROW_FIELD_BITMAP, iSIZE_COLUM_FIELD_BITMAP, false, 8, iUserFigure)) { FunctionChangesField(&cMapField[0][0], iSIZE_ROW_FIELD, iSIZE_COLUM_FIELD, iTurnKay, cFigureUser); } else { continue; } break; case 57: if(FunctionBitMapField(&iBitMapField[0][0], iSIZE_ROW_FIELD_BITMAP, iSIZE_COLUM_FIELD_BITMAP, false, 9, iUserFigure)) { FunctionChangesField(&cMapField[0][0], iSIZE_ROW_FIELD, iSIZE_COLUM_FIELD, iTurnKay, cFigureUser); } else { continue; } break; } } iPcKay = FunctionAI(&iBitMapField[0][0], iSIZE_ROW_FIELD_BITMAP, iSIZE_COLUM_FIELD_BITMAP, iUserFigure, iPCFigure); ConversionPcKay = FunctionConversionTurnPC(iPcKay); FunctionChangesField(&cMapField[0][0], iSIZE_ROW_FIELD, iSIZE_COLUM_FIELD, ConversionPcKay, cFigurePC); bWin = FunctionCheckWinner(&iBitMapField[0][0], iSIZE_ROW_FIELD_BITMAP, iSIZE_COLUM_FIELD_BITMAP, iUserFigure, iPCFigure); } switch(bWin) { case 1: ++iStatisticsWinUser; std::cout<<"Player wins"<<std::endl; break; case 2: ++iStatisticsWinPC; std::cout<<"PC wins"<<std::endl; break; case 3: ++iStatisticsNOwinner; std::cout<<"No winner"<<std::endl; break; } std::cout<<"Total wins player "<<iStatisticsWinUser<<std::endl; std::cout<<"Total wins PC "<<std::setw(5)<<iStatisticsWinPC<<std::endl; std::cout<<"Total Draws "<<std::setw(7)<<iStatisticsNOwinner<<std::endl; std::cout<<std::endl; std::cout<<"Games Played "<<iStatisticsWinUser + iStatisticsWinPC + iStatisticsNOwinner<<std::endl; std::cout<<std::endl; std::cout<<"To continue, press any key..."; _getch(); } std::cout<<"End game!!!!!!!!!!!!!!!!!!!"<<std::endl; return 0; } //=================================================================================== /* *Function menu */ //=================================================================================== void FunctionDrawMenu(void) { char cMessageText[] = " Welcome to the game of Tic Tac Toe "; // Text in the frame int iSizeMessageText = sizeof(cMessageText)/sizeof(char); for(int i = 0; i < iSizeMessageText+1; ++i)// The first line in the frame { std::cout<<'#'; } std::cout<<std::endl; for(int i = 0; i < iSizeMessageText; ++i) { if(i == 0)//bezel frame { std::cout<<'#'; for(int j = 0; j < iSizeMessageText-1; ++j) { std::cout<<' '; } std::cout<<'#'; std::cout<<std::endl; std::cout<<'#'; } if(*(cMessageText + i) != 0) { std::cout<<*(cMessageText + i); //Output text in the frame } else////bezel frame { std::cout<<'#'; std::cout<<std::endl; std::cout<<'#'; for(int j = 0; j < iSizeMessageText-1; ++j) { std::cout<<' '; } std::cout<<'#'; } } std::cout<<std::endl; for(int i = 0; i < iSizeMessageText+1; ++i)// The last line in the frame { std::cout<<'#'; } std::cout<<std::endl; std::cout<<std::endl; std::cout<<"Proposal selection: "<<std::endl; std::cout<<std::endl; std::cout<<"a) Endless game "<<std::endl; std::cout<<std::endl; std::cout<<"b) A limited number of games "<<std::endl; std::cout<<std::endl; std::cout<<"Enter your choice "; std::cout<<std::endl; } //=================================================================================== /* *Function of drawing the playing field */ //=================================================================================== void FunctionDrawField(char *cArray, int iArraySizeRow, int iArraySizeColum) { for(int iRow = 0; iRow < iArraySizeRow; ++iRow) { switch(iRow) { //------- case 0: case 2: case 4: case 6: for(int iColum = 0; iColum < iArraySizeColum; ++iColum) { //cArray[iRow][iColum]; *(cArray + ((iArraySizeColum * iRow)+iColum)) = '-'; } break; //| | | | case 1: case 3: case 5: /* *An amendment to a number of */ int iCounter = 49; switch(iRow) { case 1: iCounter += 6; break; case 3: iCounter += 3; break; } for(int iColum = 0; iColum < iArraySizeColum; ++iColum) { //cArray[iRow][iColum]; if(iColum%2 != 0) { *(cArray + ((iArraySizeColum * iRow)+iColum)) = (char)iCounter; ++iCounter; } else { *(cArray + ((iArraySizeColum * iRow)+iColum)) = '|'; } } break; } } } //=================================================================================== /* *Function of drawing the playing field */ //=================================================================================== void FunctionViewField(char *cArray, int iArraySizeRow, int iArraySizeColum) { for(int iRow = 0; iRow < iArraySizeRow; ++iRow) { for(int iColum = 0; iColum < iArraySizeColum; ++iColum) { std::cout<<*(cArray + ((iArraySizeColum * iRow)+iColum)); } std::cout<<std::endl; } } //=================================================================================== /* *Function of filling an array of maps of the field, to calculate the moves of players */ //=================================================================================== bool FunctionBitMapField(int *cArray, int iArraySizeRow, int iArraySizeColum, bool bZeroing, int iCellNumber, int iXOrToe) { bool bFlagsTurnOK = false; switch(iCellNumber) { case 1: if(*(cArray + 6) == 0) { *(cArray + 6) = iXOrToe; bFlagsTurnOK = true; } break; case 2: if(*(cArray + 7) == 0) { *(cArray + 7) = iXOrToe; bFlagsTurnOK = true; } break; case 3: if(*(cArray + 8) == 0) { *(cArray + 8) = iXOrToe; bFlagsTurnOK = true; } break; case 4: if(*(cArray + 3) == 0) { *(cArray + 3) = iXOrToe; bFlagsTurnOK = true; } break; case 5: if(*(cArray + 4) == 0) { *(cArray + 4) = iXOrToe; bFlagsTurnOK = true; } break; case 6: if(*(cArray + 5) == 0) { *(cArray + 5) = iXOrToe; bFlagsTurnOK = true; } break; case 7: if(*(cArray + 0) == 0) { *(cArray + 0) = iXOrToe; bFlagsTurnOK = true; } break; case 8: if(*(cArray + 1) == 0) { *(cArray + 1) = iXOrToe; bFlagsTurnOK = true; } break; case 9: if(*(cArray + 2) == 0) { *(cArray + 2) = iXOrToe; bFlagsTurnOK = true; } break; } if(bZeroing) { for(int iRow = 0; iRow < iArraySizeRow; ++iRow) { for(int iColum = 0; iColum < iArraySizeColum; ++iColum) { *(cArray + ((iArraySizeColum * iRow)+iColum)) = 0; } } } if(bFlagsTurnOK) { return true; } else { return false; } } //=================================================================================== /* *Function changes the values in the input field */ //=================================================================================== void FunctionChangesField(char *cArray, int iArraySizeRow, int iArraySizeColum, char cCaseChanges, char cXOrToe) { for(int iRow = 0; iRow < iArraySizeRow; ++iRow) { for(int iColum = 0; iColum < iArraySizeColum; ++iColum) { if(cCaseChanges == *(cArray + ((iArraySizeColum * iRow)+iColum))) { *(cArray + ((iArraySizeColum * iRow)+iColum)) = cXOrToe; } } } } //=================================================================================== /* *----------------------Testing function--------------------------------- *Function of drawing the playing bit map */ //=================================================================================== void FunctionViewBitMap(int *cArray, int iArraySizeRow, int iArraySizeColum) { for(int iRow = 0; iRow < iArraySizeRow; ++iRow) { for(int iColum = 0; iColum < iArraySizeColum; ++iColum) { std::cout<<*(cArray + ((iArraySizeColum * iRow)+iColum)); } std::cout<<std::endl; } } //=================================================================================== /* *Conversion function return of turn PC to the encoding keys */ //=================================================================================== int FunctionConversionTurnPC(int iKayConversion) { int iReturnKayCode; switch(iKayConversion) { case 1: iReturnKayCode = 55; break; case 2: iReturnKayCode = 56; break; case 3: iReturnKayCode = 57; break; case 4: iReturnKayCode = 52; break; case 5: iReturnKayCode = 53; break; case 6: iReturnKayCode = 54; break; case 7: iReturnKayCode = 49; break; case 8: iReturnKayCode = 50; break; case 9: iReturnKayCode = 51; break; default: iReturnKayCode = 0; } return iReturnKayCode; } //=================================================================================== /* *Function AI */ //=================================================================================== int FunctionAI(int *iArrBitMap, int iArraySizeRow, int iArraySizeColum, int iUserFigure, int iPcSymbolTurn) { /* *Check the horizontal lines */ for(int i = 0, iCheck = 0; i < iArraySizeRow; ++i) { for(int j = 0; j < iArraySizeColum; ++j) { iCheck += *(iArrBitMap + ((iArraySizeColum * i) + j)); if(iCheck == iUserFigure * 2) { for(int k = 0; k < iArraySizeColum; ++k) { if(0 == *(iArrBitMap + ((iArraySizeColum * i) + k))) { *(iArrBitMap + ((iArraySizeColum * i) + k)) = iPcSymbolTurn; return (iArraySizeColum * i + k) + 1; } } } } iCheck = 0; } /* *Check the vertical lines */ for(int i = 0, iCheck = 0; i < iArraySizeRow; ++i) { for(int j = 0; j < iArraySizeColum; ++j) { iCheck += *(iArrBitMap + ((iArraySizeColum * j) + i)); if(iCheck == iUserFigure * 2) { for(int k = 0; k < iArraySizeColum; ++k) { if(0 == *(iArrBitMap + ((iArraySizeColum * k) + i))) { *(iArrBitMap + ((iArraySizeColum * k) + i)) = iPcSymbolTurn; return (iArraySizeColum * k + i) + 1; } } } } iCheck = 0; } /* *Check the diagonal lines */ bool bFlagsRight = false; for(int i = 0, iCheck = 0; i < iArraySizeRow; ++i) { iCheck += *(iArrBitMap + ((iArraySizeColum * i) + i)); if(iCheck == iUserFigure * 2) { bFlagsRight = true; break; } } bool bFlagsLeft = false; for(int i = 2, iCheck = 0, j = 0; j < iArraySizeColum; --i, ++j ) { iCheck += *(iArrBitMap + ((iArraySizeColum * i) + j)); if(iCheck == iUserFigure * 2) { bFlagsLeft = true; break; } } if(bFlagsLeft) { for(int i = 2, iCheck = 0, j = 0; j < iArraySizeColum; --i, ++j ) { if(*(iArrBitMap + ((iArraySizeColum * i) + j)) == 0) { *(iArrBitMap + ((iArraySizeColum * i) + j)) = iPcSymbolTurn; return (iArraySizeColum * i + j) + 1; } } } if(bFlagsRight) { for(int i = 0, iCheck = 0; i < iArraySizeRow; ++i) { if(*(iArrBitMap + ((iArraySizeColum * i) + i)) == 0) { *(iArrBitMap + ((iArraySizeColum * i) + i)) = iPcSymbolTurn; return (iArraySizeColum * i + i) + 1; } } } /* *Checking whether the busy center */ if(*(iArrBitMap + ((iArraySizeColum * 1) + 1)) == 0) { *(iArrBitMap + ((iArraySizeColum * 1) + 1)) = iPcSymbolTurn; return (iArraySizeColum * 1 + 1) + 1; } else { /* *Turn any free space */ for(int i = 0; i < iArraySizeRow; ++i) { for(int j = 0; j < iArraySizeColum-1; j += 2) { if(*(iArrBitMap + ((iArraySizeColum * i) + j)) == 0) { *(iArrBitMap + ((iArraySizeColum * i) + j)) = iPcSymbolTurn; return (iArraySizeColum * i + j) + 1; } } } } return 0; } //=================================================================================== /* *Function to check whether there is the winner */ //=================================================================================== int FunctionCheckWinner(int *iArrBitMap, int iArraySizeRow, int iArraySizeColum, int iUserSymbolTurn, int iPcSymbolTurn) { /* *Check the horizontal lines */ for(int i = 0, iCheck = 0; i < iArraySizeRow; ++i) { for(int j = 0; j < iArraySizeColum; ++j) { iCheck += *(iArrBitMap + ((iArraySizeColum * i) + j)); if(iCheck == iUserSymbolTurn * 3) { return 1; } else if(iCheck == iPcSymbolTurn * 3) { return 2; } } iCheck = 0; } /* *Check the vertical lines */ for(int i = 0, iCheck = 0; i < iArraySizeRow; ++i) { for(int j = 0; j < iArraySizeColum; ++j) { iCheck += *(iArrBitMap + ((iArraySizeColum * j) + i)); if(iCheck == iUserSymbolTurn * 3) { return 1; } else if(iCheck == iPcSymbolTurn * 3) { return 2; } } iCheck = 0; } /* *Check the diagonal lines */ for(int i = 0, iCheck = 0; i < iArraySizeRow; ++i) { iCheck += *(iArrBitMap + ((iArraySizeColum * i) + i)); if(iCheck == iUserSymbolTurn * 3) { return 1; } else if(iCheck == iPcSymbolTurn * 3) { return 2; } } for(int i = 2, iCheck = 0, j = 0; j < iArraySizeColum; --i, ++j ) { iCheck += *(iArrBitMap + ((iArraySizeColum * i) + j)); if(iCheck == iUserSymbolTurn * 3) { return 1; } else if(iCheck == iPcSymbolTurn * 3) { return 2; } } /* *Check whether there is general winner */ for(int i = 0, iCaunterWin = 0; i < iArraySizeRow; ++i) { for(int j = 0; j < iArraySizeColum; ++j) { if(*(iArrBitMap + ((iArraySizeColum * i) + j)) != 0) { ++iCaunterWin; } if(iCaunterWin == iArraySizeRow * iArraySizeColum) { return 3; } } } return 0; }
Игра крестики нолики
Сообщений 1 страница 2 из 2
Поделиться103-04-2011 22:59:02
Поделиться214-07-2023 18:23:49
Vict277.2BettCHAPUnivSachIntrMichJuansselKhalDisnElegNestXVIIOrieXVIIClueMartElseZoneAudi
GuntAtlaFiskTescMastDermNaivErbaThisGeorJohnChamSongCaldGarnBrauCoasPantStepEpilDiadArma
ElisPumaSplaJeweSideCedrAmarMariJeweRaoumattgunmSampIsaaSabaJohnWongMissKatrFeliviscSela
CircAlleAireEnhaStilXVIIMayaOSCAPariMiguenriHideMileDolbLifeApasSwarFuxiMaryNovaRaquMiyo
ZoneCambArmiZoneZoneFranSwardiamLionAgatWorlRobeNintXVIIStepFyodFritGigaJetAFridRudoAero
CarlRodrHughJuliKaroYourVillHoteModeARIMClauHansCataSallNASCClivSpikChicMiraMicrReasBARO
UltiwwwbIKCOAmerGrifOrchValiNDFEStroThomHauttoucOZONJohnWindSincNuanBoscIronDuckCesaChas
EasiAnasnoteFirsMartCathPublSpeeTakePierVIIIBranFromFyodJeweEditClanXIIIJohnJohnJeanDick
VIVALushColuMetaconcKennArtiBillJeweKennWallAllaHaraRajsAlanAllagrouDiliWestWilhFinaKari
ISBNTuttcepoNiagChloLaurinteModeModeModeOrchVariTacoHappEvanRiseEnjoBabiTimeGuitPapeCity
tuchkashighPart