Question

In: Computer Science

I'm having trouble programming connect four board game using linked lists in c++. Can you code...

I'm having trouble programming connect four board game using linked lists in c++. Can you code connect four game using linked lists.

Solutions

Expert Solution

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;
}

Related Solutions

I'm having trouble programming connect four board game using linked lists, sets and maps in c++....
I'm having trouble programming connect four board game using linked lists, sets and maps in c++. Can you code connect four game using these concepts.
Can you write a program for snakes and ladder board game using linked lists in c++
Can you write a program for snakes and ladder board game using linked lists in c++
Can you write a program for the card game WAR using linked lists in c++!
Can you write a program for the card game WAR using linked lists in c++!
Can you program Exploding kittens card game in c++ using linked lists and classes! The game...
Can you program Exploding kittens card game in c++ using linked lists and classes! The game is played with between 2 and 4 players. You'll have a deck of cards containing some Exploding Kittens. You play the game by putting the deck face down and taking turns drawing cards until someone draws an Exploding Kitten. When that happens, that person explodes. They are now dead and out of the game. This process continues until there's only one player left, who...
I am having trouble with a C++ code that I'm working on. It is a spell...
I am having trouble with a C++ code that I'm working on. It is a spell checker program. It needs to compare two arrays, a dictionary, and an array with misspelled strings that are compared to the strings in the dictionary. the strings that are in the second array that is not in the Dictionary are assumed to be misspelled. All of the strings in the dictionary are lowercase without any extra characters so the strings that are passed into...
I'm having trouble understanding the following code (a snippet of a code). What does it do?...
I'm having trouble understanding the following code (a snippet of a code). What does it do? The whole code is about comparing efficiencies of different algorithms. def partition(list,first,last): piv = list[first] lmark = first+1 rmark = last done = False while not done: while lmark <= rmark and list[lmark]<=piv: lmark=lmark+1 while list[rmark]>=piv and rmark>=lmark: rmark=rmark-1 if rmark<lmark: done = True else: temp = list[lmark] list[lmark]=list[rmark] list[rmark]=temp temp = list[first] list[first]=list[rmark] list[rmark]=temp return rmark
Using dev c++ I'm having trouble with classes. I think the part that I am not...
Using dev c++ I'm having trouble with classes. I think the part that I am not understanding is sending data between files and also using bool data. I've been working on this program for a long time with many errors but now I've thrown in my hat to ask for outside help. Here is the homework that has given me so many issues: The [REDACTED] Phone Store needs a program to compute phone charges for some phones sold in the...
I'm having trouble thinking of a way that I can delete duplicates from a c string...
I'm having trouble thinking of a way that I can delete duplicates from a c string of alphabets. So what I'm supposed to do is I'm supposed to delete the repeated letters in the c string using pointers and also dynamically allocating space. I'm guessing that I will have to dynamically allocate space for an array to put in the letters that only appear once. To do that, can I create 2 pointers in a function and have 1 pointer...
TCP client and server using C programming I am having trouble on how to read in...
TCP client and server using C programming I am having trouble on how to read in the IP adress and port number from the terminal Example: Enter IP address: 127.0.0.1 Enter Port Number: 8000 in both client and server code. How do can I make I can assign the Ip address and port number using the example above. the error I get is that the client couldn't connect with the server whenever i get the port number from the user...
I'm having trouble creating a histogram using openCV (color segmentation)
I'm having trouble creating a histogram using openCV (color segmentation)
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT