How to shorten my code

Hi, I am just wandering what possible ways I could go about shortening my code, I believe it is a bit lengthy and am wanting it as short as could any ideas?

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
	//checking if 3 in row for Player 1 (X)
	//rows
	if (grid[0][0] == 'X' && grid[0][1] == 'X' && grid[0][2] == 'X')
		return 'X';
	else if (grid[1][0] == 'X' && grid[1][1] == 'X' && grid[1][2] == 'X')
		return 'X';
	else if (grid[2][0] == 'X' && grid[2][1] == 'X' && grid[2][2] == 'X')
		return 'X';

	//columns
	else if (grid[0][0] == 'X' && grid[1][0] == 'X' && grid[2][0] == 'X')
		return 'X';
	else if (grid[0][1] == 'X' && grid[1][1] == 'X' && grid[2][1] == 'X')
		return 'X';
	else if (grid[0][2] == 'X' && grid[1][2] == 'X' && grid[2][2] == 'X')
		return 'X';

	//diagonals
	else if (grid[0][0] == 'X' && grid[1][1] == 'X' && grid[2][2] == 'X')
		return 'X';
	else if (grid[2][0] == 'X' && grid[1][1] == 'X' && grid[0][2] == 'X')
		return 'X';

	//checking if 3 in row for Player 2 (O)
	//rows
	else if (grid[0][0] == 'O' && grid[0][1] == 'O' && grid[0][2] == 'O')
		return 'O';
	else if (grid[1][0] == 'O' && grid[1][1] == 'O' && grid[1][2] == 'O')
		return 'O';
	else if (grid[2][0] == 'O' && grid[2][1] == 'O' && grid[2][2] == 'O')
		return 'O';
	//columns
	else if (grid[0][0] == 'O' && grid[0][1] == 'O' && grid[2][0] == 'O')
		return 'O';
	else if (grid[0][1] == 'O' && grid[1][1] == 'O' && grid[2][1] == 'O')
		return 'O';
	else if (grid[0][2] == 'O' && grid[1][2] == 'O' && grid[2][2] == 'O')
		return 'O';
	//diagonals
	else if (grid[0][0] == 'O' && grid[1][1] == 'O' && grid[2][2] == 'O')
		return 'O';
	else if (grid[2][0] == 'O' && grid[1][1] == 'O' && grid[0][2] == 'O')
		return 'O';
	//return default if all fields filled in (tie)
	else
		return '/';
1
2
3
4
5
6
	if ((grid[0][0] == 'X' && grid[0][1] == 'X' && grid[0][2] == 'X') ||
                     (grid[1][0] == 'X' && grid[1][1] == 'X' && grid[1][2] == 'X') ||
                     (grid[2][0] == 'X' && grid[2][1] == 'X' && grid[2][2] == 'X') )
                return 'X';

//when all statements return the same thing from a control they can usually be combines 
Topic archived. No new replies allowed.