Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <limits>
- char gameboard[3][3] = {{'7','8','9'},
- {'4','5','6'},
- {'1','2','3'}};
- char playerSymbols[] = {'X','O'};
- void drawboard();
- void processPlayerMove(int);
- int getCoord();
- int xFromCoord(int coord);
- int yFromCoord(int coord);
- int main(){
- std::cout << "***TIC TAC TOE***" << std::endl;
- drawboard();
- for (int i = 0; i < 10; i++){
- processPlayerMove(0);
- drawboard();
- }
- }
- void drawboard(){
- for (int i = 0; i < 3; ++i){
- for (int k = 0; k < 3; ++k){
- std::cout << " " << gameboard[i][k];
- if (k < 2){
- std::cout << " |";
- }
- }
- std::cout << std::endl;
- if (i < 2){
- std::cout << "---+---+---" << std::endl;
- }
- }
- }
- void processPlayerMove(int playerNumber){
- std::cout << "Player " << playerNumber+1 << " choose where to place \'" <<
- playerSymbols[playerNumber] << "\'" << std::endl;
- int targetCoord = getCoord();
- while (gameboard[yFromCoord(targetCoord)][xFromCoord(targetCoord)] != (targetCoord+'0')){
- std::cout << "expected " << targetCoord << " found " <<
- gameboard[yFromCoord(targetCoord)][xFromCoord(targetCoord)] << std::endl;
- std::cout << "That spot is occupied. Please choose another spot." << std::endl;
- targetCoord = getCoord();
- }
- gameboard[yFromCoord(targetCoord)][xFromCoord(targetCoord)] =playerSymbols[playerNumber];
- }
- int getCoord(){
- char userIn;
- do {
- std::cin >> userIn;
- }while (userIn < '0' || userIn > '9');
- std::cin.clear();
- std::cin.ignore(std::numeric_limits<std::streamsize>::max(),'\n');
- return userIn-'0';
- }
- int yFromCoord(int coord){
- int zeroedCoord = coord-1;
- zeroedCoord = 8-zeroedCoord;
- return zeroedCoord/3;
- }
- int xFromCoord(int coord){
- int zeroedCoord = coord-1;
- return zeroedCoord%3;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement