In: Computer Science
I'm having trouble programming connect four board game using linked lists in c++. Can you code connect four game using linked lists.
Code:
// main.cpp #include <iostream> #include "tictactoe.h" int main() { TicTacToe Game; Game.welcome(); Game.drawBoard(); do { Game.printTurn(); if (Game.playerHuman()) // human turn? Game.humanMove(); else Game.computerMove(); Game.drawBoard(); Game.nextTurn(); } while (!Game.winner() && !Game.fullBoard()); return 0; }
Code:
// tictactoe.h #ifndef TICTACTOE_H #define TICTACTOE_H class TicTacToe { private: int board[30][30]; // board[x][y] bool droppieces; // connect4 style pieces drop int xMax; // max horizontal board size int yMax; // max vertical board size int rowSize; // need in a row to win int player; // current player int totalTurns; // how many turns so far? int maxTurns; // full board int numberPlayers; // 1 to 20 bool playerType[9]; // true = human, false = comp public: TicTacToe(); void welcome(); // welcome screen void printTurn(); // whos turn? bool playerHuman(); // is player human? void humanMove(); // human controls void computerMove(); // computer strategy void drawBoard(); // display board bool winner(); // is there a winner? bool fullBoard(); // is the board full? void nextTurn(); // switch turn void placePieces(int x, int y); // move peices void announceWinner(int winner); // winner!! }; #endif // TICTACTOE_H
Code:
// tictactoe.cpp #include <iostream> #include <stdio.h> #include <stdlib.h> #include <time.h> #include "tictactoe.h" TicTacToe::TicTacToe() { srand (time(0)); // randomize timer player = 1; // who starts? xMax = 15; // x size of gameboard yMax = 10; // y size of gameboard rowSize = 4; // how many in a row to win? droppieces = true; // 1 = pieces drop, 0 = stay // new player setup numberPlayers = 2; // how many players? playerType[1] = 1; // player # 1 is human (1) playerType[2] = 0; // player # 2 is comp (0) //playerType[3] = 0; // player # 3 is comp (0) totalTurns = 0; // used for boardFull() maxTurns = xMax * yMax; // (total board spaces) // format game board for (int y = 0; y < yMax; y++) for (int x = 0; x < xMax; x++) board[x][y] = 0; } void TicTacToe::welcome() { std::cout << "Welcome to the connect " << rowSize << " game!\n"; } void TicTacToe::drawBoard() { std::cout << std::endl; for (int y = 0; y < yMax; y++) { // draw game board and pieces for (int x = 0; x < xMax; x++) { // no piece then just draw space if (!board[x][y]) std::cout << " "; else std::cout << " " << board[x][y] << " "; // draw seperator if not end if (x < xMax - 1) std::cout << "│"; } std::cout << std::endl; // draw seperator line between board for (int z = 0; z < xMax; z++) { // draw seperator if not end if (y < yMax - 1) { std::cout << "───"; // draw connection if not end if (z < xMax - 1) std::cout << "┼"; } } std::cout << std::endl; } } void TicTacToe::printTurn() { std::cout << "Player " << player << "'s turn.\n"; } void TicTacToe::nextTurn() { totalTurns++; // start again at first player if (++player > numberPlayers) player = 1; } bool TicTacToe::playerHuman() { return playerType[player]; } void TicTacToe::humanMove() { int moveX, moveY = 0; do { std::cout << "\nEnter x: "; std::cin >> moveX; // -1 for computer assisted move if (moveX == -1) break; if (!droppieces) { std::cout << "\nEnter y: "; std::cin >> moveY; moveY--; // compensate for user } moveX--; // compensate for user } while (moveX < 0 || moveX > xMax - 1 || moveY < 0 || moveY > yMax - 1 || board[moveX][moveY]); if (moveX == -1) computerMove(); else placePieces(moveX, moveY); } void TicTacToe::computerMove() { int moveX, moveY; do { moveX = rand() % xMax; // pick a random spot moveY = rand() % yMax; // pick a random spot } while (board[moveX][moveY]); // loop if taken placePieces(moveX, moveY); } void TicTacToe::placePieces(int x, int y) { if (droppieces) { while (y++ < yMax - 1 && !board[x][y]){} y--; // the last place was taken so go back one } board[x][y] = player; } bool TicTacToe::winner() { int matchcount = 0; int lastmatch = 0; // check x row for (int y = 0; y < yMax; y++) for (int x = 0; x + rowSize - 1 < xMax; x++) { matchcount = 0; lastmatch = board[x][y]; // check through columns for (int z = 0; z < rowSize; z++) if (board[x+z][y] && board[x+z][y] == lastmatch && ++matchcount == rowSize) { announceWinner(lastmatch); return true; } } // check y row for (int y = 0; y + rowSize - 1 < yMax; y++) for (int x = 0; x < xMax; x++) { matchcount = 0; lastmatch = board[x][y]; // check through rows for (int z = 0; z < rowSize; z++) if (board[x][y+z] && board[x][y+z] == lastmatch && ++matchcount == rowSize) { announceWinner(lastmatch); return true; } } // diagonal row check: top left - bottom right // 10000 // 01000 // 00100 // 00000 // 00000 // move through columns for (int y = 0; y + rowSize - 1 < yMax; y++) for (int x = 0; x < xMax; x++) { matchcount = 0; lastmatch = board[x][y]; // check through rows for (int z = 0; z < rowSize; z++) if (board[x+z][y+z] && board[x+z][y+z] == lastmatch && ++matchcount == rowSize) { announceWinner(lastmatch); return true; } } // diagonal row check: top right - bottom left // 00001 // 00010 // 00100 // 00000 // 00000 // move through columns for (int y = 0; y + rowSize <= yMax; y++) for (int x = xMax - 1; x - rowSize + 1 >= 0; x--) { matchcount = 0; lastmatch = board[x][y]; // check through rows for (int z = 0; z < rowSize; z++) if (board[x-z][y+z] && board[x-z][y+z] == lastmatch && ++matchcount == rowSize) { announceWinner(lastmatch); return true; } } return false; } void TicTacToe::announceWinner(int winner) { std::cout << "\nPlayer " << winner << " wins!\n\n"; } bool TicTacToe::fullBoard() { if (totalTurns == maxTurns) { std::cout << "\nTie game!\n\n"; return true; } return false; }