C Introduction to Programming It is short and simple practic
C++ Introduction to Programming
It is short and simple practice.
Functions and Classes
Please solve #6, 7, 8, 9, 10
#include<iostream>
#include<vector>
..
using namespace std;
6. Let\'s write a 4x4 tic-tac-toe game. Here\'s a class definition for the board: class ttt board public: int board[4][4]; Assuming that 0 means an unmarked space, 1 means X, and -1 means o, write the function set which sets a space (given by its Row and Column) on a board to a given marker: void set(ttt board& b, int c, int r, int mark) \\ / YOUR ANSWER HERE 7. Continuing the previous, complete the following functions which test locations on the board to see whether they are empty, Xs, or Os bool is empty(ttt board& b, int c, int r) /I YOUR ANSWER HERE bool is x(ttt board& b, int c, int r) f YOUR ANSWER HERE bool is_o(ttt_board& b, int c, int r) ( / YOUR ANSWER HERE 8. Using the functions you wrote in the previous, write the following functions which test whether there are four-in-a-row along a column, or a row. If there are four Xs the function should return 1, if there are four Os it should return -1, otherwise it should return e // Check all rows for 4-in-a-row int has_4_row(ttt_board& b) f YOUR ANSWER HERE // Check all columns for 4-in-a-column int has 4 col(ttt board& b / YOUR ANSWER HERE 9. Write a function , has-4, diagonal, which checks both diagonals, returning 1, -1, or 0 as for the other has 4. functions. int has 4 diagonal(ttt board& b // YOUR ANSWER HERE 10. Write a function print-board\" which prints out a ttt-board\' to the screen, as blank spaces, Xs, and Os. Include the row/column numbers along the left and top edges. E.g., a board might print as 1234 1X 00 20X X 3 X00 400 X If you want to print lines between the rows/columns to make things easier to read, you can do that, in which case the above might look like But that\'s optional. void print board(ttt board b // YOUR ANSWER HERE Puzzle: complete the 4-way TTT game so that it can read in moves from the users and will properly detect a win.Solution
Following is the required code:
#include <iostream>
 #include <vector>
 using namespace std;
class ttt_board{
 public:
    int board[4][4];
 };
void set( ttt_board& b, int c, int r, int mark ){
    //doesnt check postion at r,c was already marked
    b.board[r][c] = mark;
 }
bool is_empty( ttt_board& b, int c, int r ){
    //true if it is at r,c
    return b.board[r][c]==0;
 }
bool is_x( ttt_board& b, int c, int r ){
    return b.board[r][c]==1;
 }
bool is_o( ttt_board& b, int c, int r ){
    return b.board[r][c]==-1;
 }
int has_4_row( ttt_board& b ){
    for( int row=0; row < 4; row++ ){
        if( is_x(b,row,0) && is_x(b,row,1) && is_x(b,row,2) && is_x(b,row,3) ){
            return 1;
        }
        if( is_o(b,row,0) && is_o(b,row,1) && is_o(b,row,2) && is_o(b,row,3) ){
            return -1;
        }
    }
    return 0;
 }
int has_4_col( ttt_board& b ){
    for( int col=0; col < 4; col++ ){
        if( is_x(b,0,col) && is_x(b,1,col) && is_x(b,2,col) && is_x(b,3,col) ){
            return 1;
        }
        if( is_o(b,0,col) && is_o(b,1,col) && is_o(b,2,col) && is_o(b,3,col) ){
            return -1;
        }
    }
    return 0;
 }
int has_4_diagonal( ttt_board &b ){
    //check both diagonal
    if( is_x(b,0,0) && is_x(b,1,1) && is_x(b,2,2) && is_x(b,3,3) ){
        return 1;
    }
    if( is_x(b,0,3) && is_x(b,1,2) && is_x(b,2,1) && is_x(b,3,0) ){
        return 1;
    }
    if( is_o(b,0,0) && is_o(b,1,1) && is_o(b,2,2) && is_o(b,3,3) ){
        return -1;
    }
    if( is_o(b,0,3) && is_o(b,1,2) && is_o(b,2,1) && is_o(b,3,0) ){
        return -1;
    }
    return 0;
 }
void print_board( ttt_board b ){
    cout << \" 1234\" << endl;
    for(int i =0; i < 4; i++){
        cout << i+1;
        for(int j =0; j<4; j++){
            if( b.board[i][j] == 1){cout<<\'X\';}
            else if( b.board[i][j] == -1){cout<<\'0\';}
            else{cout<<\' \';}
        }
        cout << endl;
    }
 }
int main(){
    int row, col;
    ttt_board b;
    for(int i =0; i< 4; i++){
        for(int j = 0; j < 4; j++){
            set( b, i, j , 0);
        }
    };
    int moves = 0;
    while( moves < 16 ){
        moves+= 2;
        cout << \"First Player\'s turn\" << endl;
        while( true ){
            cout << \"Enter your move, as two integers with spaces: row col\"<< endl;
            cin >> row >> col;
            if( (col>=0 && col<4) && (row>=0 && row<4) && is_empty(b, col, row )){ break; }
            else{ cout << \"Invalid move, enter again\" << endl;}
        }
        set( b, col ,row, -1);
        print_board(b);
        if( has_4_row(b) || has_4_col(b) || has_4_diagonal(b) ){
            cout << \"First player wins\" << endl;
            return 0;
        }
       cout << \"Second Player\'s turn\" << endl;
        while( true ){
            cout << \"Enter your move, as two integers with spaces: row col\"<< endl;
            cin >> row >> col;
            if( is_empty(b, col , row)){ break; }
            else{ cout << \"Invalid move, enter again\" << endl;}
        }
        set( b, col , row, 1);
        print_board(b);
        if( has_4_row(b) || has_4_col(b) || has_4_diagonal(b) ){
            cout << \"Second player wins\" << endl;
            return 0;
        }
    }
    return 0;
 }



