Difference: CodeClass (3 vs. 4)

Revision 42018-03-01 - JimSkon

Line: 1 to 1
 
META TOPICPARENT name="WebHome"

Classes

Added:
>
>

Tic Tac Toe

<-- SyntaxHighlightingPlugin -->
#include <iostream>
using namespace std;
void displayBoard(char board[3][3]);
void nextMove(char player, char board[][3]);
bool notValid(int r, int c);
bool inUse(int r, int c, char board[3][3]);
bool Win(char board[3][3]);

int main() {
    char player = 'X';
    char aBoard[3][3] = {' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '};
    while (!Win(aBoard)) {
        nextMove(player, aBoard);
        displayBoard(aBoard);
        if (player == 'X') {
            player = 'O';
        } else {
            player = 'X';
        }
    }
    return 0;
}

void displayBoard(char board[3][3]) {
    for (int row = 0; row &lt; 3; row++) {
        for (int col = 0; col &lt; 3; col++) {
            cout &lt;&lt; board[row][col];
            if (col &lt; 2) {
                cout &lt;&lt; "|";
            }
        }
        cout &lt;&lt; endl;
        if (row &lt; 2) {
            cout &lt;&lt; "-----" &lt;&lt; endl;
        }
    }
}

void nextMove(char player, char board[][3]) {
    int row, col;
    do {
        cout &lt;&lt; "Player " &lt;&lt; player &lt;&lt; " what is your move (row column):";
        cin &gt;&gt; row &gt;&gt; col;
    } while (notValid(row, col)||inUse(row,col,board));
    board[row - 1][col - 1] = player;
}

bool notValid(int r, int c){
    bool result = r&gt;3 || r3 || c&lt;1;
    if (result) {
        cout &lt;&lt; "Error, row and colum must be between 1 and 3!" &lt;&lt; endl;
    }
    return result;
}

bool inUse(int r, int c, char board[3][3]){
    bool result = (board[r-1][c-1]!=' ');
    if (result) {
        cout &lt;&lt; "Error, space in use!" &lt;&lt; endl;
    }
    return result;
}

// Finish this function ... It may call other functions
bool Win(char board[3][3]) {
    return false;
}
 
<-- end SyntaxHighlightingPlugin -->
</sticky>
 

Candidate Class

Added:
>
>
 %CODE{"c++"}% #include using namespace std;
Line: 48 to 120
  polls = pollNum; } void Candidate :: print(){
Changed:
<
<
cout << "Name : " << name << ", "; cout << "Party : " << party << ", "; cout << "Polls : " << polls << "%" << endl;
>
>
cout << "Name : " << name << ", "; cout << "Party : " << party << ", "; cout << "Polls : " << polls << "%" << endl;
 }

int main() {

Line: 61 to 133
  h.print(); j.print(); g.print();
Changed:
<
<
cout << endl;
>
>
cout << endl;
  h.setPolls(12); j.setPolls(11); g.setPolls(77);
Line: 72 to 144
  }
Changed:
<
<
%ENDCODE%
>
>
%ENDCODE%</sticky>
 

Class with a Member Function

%CODE{"c++"}%
 
This site is powered by the TWiki collaboration platform Powered by PerlCopyright © 2008-2019 by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding TWiki? Send feedback