Posted on 28-03-2011
Filed Under (C++, programming) by admin
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#include <iostream>
#include <string>
using namespace std;
 
class TicTacToe
{
private: 
	char table[3][3];
public:
	TicTacToe() { }
	//insert numbers
	void setTable()
	{
		int n = 1;
		for(int i = 0; i < 3; i++)
		{
			for(int j = 0; j < 3; j++)
			{
				table[i][j] = '0' + n;//cast n to char
				n++;
			}
		}
	}
	//output table to screen
	void printTable()
	{
		for(int i = 0; i < 3; i++)
		{
			for(int j = 0; j < 3; j++)
				if(j < 2) { cout << table[i][j] << "|"; }
				else { cout << table[i][j] << endl; }
			if(i < 2) { cout << "-+-+-\n"; }
		}
	}
	//player move and set to X or O
	void playerMove(char num, char player)
	{
		bool wrongMove = true;//wrong move
		for(int i = 0; i < 3; i++)
		{
			for(int j = 0; j < 3; j++)
			{//mark square with X or O
				if(table[i][j] == num) { table[i][j] = player; wrongMove = false; }
			}
		}
		if(wrongMove == true) { cout << "Wrong move!\n"; }
	}
	//cheking for winers
	bool checkWinner(char player, bool gameOver)
	{
		//winning conditions
		for(int i = 0; i < 3; i++)//checking rows
			if(table[i][0] == table[i][1] && table[i][1] == table[i][2]) gameOver = true;
		for(int i = 0; i < 3; i++)//checking columns
			if(table[0][i] == table[1][i] && table[1][i] == table[2][i]) gameOver = true;
		//diagonals
		if(table[0][0] == table[1][1] && table[1][1] == table[2][2]) gameOver = true;
		if(table[0][2] == table[1][1] && table[1][1] == table[2][0]) gameOver = true;
		if(gameOver == true) 
		{
			cout << "Player " << player << " wins!\n\n";
		}
		return gameOver;
	}
	//checking for a draw
	bool checkDraw(bool gameOver)
	{
		int n = 1, count = 0;
		for(int i = 0; i < 3; i++)
		{
			for(int j = 0; j < 3; j++)
			{
				//cheking if board is full
				if(table[i][j] == '0'+n) { count++; }
				n++;
			}
		}
		if(count < 1) { cout << "It's a draw!\n\n"; gameOver = true; }
		return gameOver;
	}
};
 
int main()
{
	bool done = false, gameOver = false;
	char player = 'O', num;
	TicTacToe myGame;
	myGame.setTable();
	do
	{
		if(player == 'X') { player = 'O'; }
		else { player = 'X'; } 
		myGame.printTable();
		cout << "Player \"" << player << "\" turn or (q) to quit> ";
		cin >> num; 
		cout << "\n";
		if(num == 'q') { cout << "Goodbye!...\n"; break; }
		myGame.playerMove(num, player);
		gameOver = myGame.checkWinner(player, gameOver);
		gameOver = myGame.checkDraw(gameOver);
		//if game over, starting over
		if(gameOver == true) 
		{	myGame.setTable(); 
			gameOver = false; 
		}
	}while(!done);
 
	system("pause");
	return 0;
}

Comments Off    Read More   
Posted on 21-04-2009
Filed Under (java, programming) by admin
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import java.util.*;
public class Tictactoe {
    private static final int ROWS = 3;
    private static final int COLUMNS = 3;
    private String[][] board;
    //construct an empty board
    public Tictactoe()
    {
        board = new String[ROWS][COLUMNS];
        //Fill with spaces
        for(int i = 0; i < ROWS; i++)
        {
            for(int j = 0; j < COLUMNS; j++)
            {
                board[i][j] = " ";
            }
        }
    }
    //set the fields in the board
    public void set(int i, int j, String player)
    {
        if(board[i][j].equals(" "))
        {
            board[i][j] = player;
        }
    }
    /**creates a string representation of the board
     * @return the string representation
     */
    public String toString()
    {
        String r = "";
        for(int i = 0; i < ROWS; i++)
        {
          r += "|";
            for(int j = 0; j < COLUMNS; j++)
            {
                r += board[i][j];
            }
          r += "|\n";
        }
      return r;
    }
    public static void main(String[] args)
    {
        Scanner in = new Scanner(System.in);
        String player = "x";
        Tictactoe game = new Tictactoe();
        boolean done = false;
        while(!done)
        {
            System.out.println(game.toString());
            System.out.println("Row for "+player+" (-1 to exit): ");
            int row = in.nextInt();
            if(row < 0) done = true;
            else
            {
                System.out.println("Column for "+player+": ");
                int column = in.nextInt();
                game.set(row, column, player);
                if(player.equals("x")) player = "o";
                else player = "x";
            }
        }
    }
}

Comments Off    Read More