Archived

This topic is now archived and is closed to further replies.

Tic Tac Toe Winner?

This topic is 5740 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I was just wondering if there is an easy way to check whether a player has won the game? I have all go's recorded in an 2D array, player[x][y].crosspos = TRUE; (or .noughtpos = TRUE. As you have probably guessed I'm new to this, so I was also wondering if anyone could make any suggestions on ways I can optimise/improve my code (I'm using Allegro, and VC++).
#include <stdlib.h>
#include <stdio.h>

#include "allegro.h"

void SetupBoard();
void SetSelected();
void DrawPos();
void GetMouseClick();
void DrawNorc();
void DrawCross(int x, int y);
void DrawNought(int a, int b);
void ClearArray();

int red, white, black, blue;
int selx, sely;
bool PlayerGo;

BITMAP *buffer;
BITMAP *nought;
BITMAP *cross;
PALETTE pat_nought;
PALETTE pat_cross;

class Player
{
	public:
	  bool crosspos;
	  bool noughtpos;
};

Player player[3][3];

int main(int argc, char* argv[])
{
	allegro_init();
	install_keyboard();
	install_mouse();
	
	set_color_depth(16);

	if (set_gfx_mode(GFX_AUTODETECT, 640, 480, 0, 0) != 0) 
	{
		set_gfx_mode(GFX_TEXT, 0, 0, 0, 0);
		allegro_message("Unable to set any graphic mode\n%s\n", allegro_error);
		return 1;
	}
	ClearArray();
	buffer = create_bitmap(SCREEN_W, SCREEN_H);
	nought = load_bitmap("nought.bmp", pat_nought);
    if (!nought) {
      set_gfx_mode(GFX_TEXT, 0, 0, 0, 0);
      allegro_message("Error reading bitmap file - nought.bmp");
      return 1;
    }
	cross = load_bitmap("cross.bmp", pat_cross);
	red = makecol(255, 0, 0);
	white = makecol(255, 255, 255);
	PlayerGo = TRUE;
	
	do 
	{
		clear_to_color(buffer, white);
		
		SetupBoard();
		
		SetSelected();
		GetMouseClick();
		DrawPos();
		DrawNorc();
		show_mouse(buffer);
		vsync();
		blit(buffer, screen, 0, 0, 0, 0, SCREEN_W, SCREEN_H);
		
	} while (!key[KEY_ESC]);
		
		

	return 0;
		
}
END_OF_MAIN()

void SetupBoard()
{
	black = makecol(0, 0, 0);
    line(buffer, 273, 135, 273, 345, black);
	line(buffer, 366, 135, 366, 345, black);
	line(buffer, 180, 205, 460, 205, black);
	line(buffer, 180, 275, 460, 275, black);
}

void SetSelected()
{
	selx = (mouse_x - 180) / 93;
	sely = (mouse_y - 135) / 70;
}

void DrawPos()
{
	int drawx, drawy;
	blue = makecol(0, 0, 255);

	if (selx < 3 && selx > - 1 && sely < 3 && sely > -1)
	{	
		drawx = (selx * 93) + 180;
		drawy = (sely * 70) + 135;

		rect(buffer, drawx, drawy, drawx + 93, drawy + 70, blue); 
	}
}

void GetMouseClick()
{
	if (selx < 3 && selx > - 1 && sely < 3 && sely > -1)
	{
		if(!(player[selx][sely].crosspos || player[selx][sely].noughtpos))
		{
			if (mouse_b & 1)
			{
				if (PlayerGo)
				{
					player[selx][sely].noughtpos = TRUE;
					PlayerGo = FALSE;
				}
				else
				{
					player[selx][sely].crosspos = TRUE;
					PlayerGo = TRUE;
				}
			}
		}
	}
}

void DrawNorc()
{
	int i, j;
	for (i = 0; i < 3; i++)
	{
		
		for (j = 0; j < 3; j++)
		{	
			
			
			if (player[j].crosspos)
				DrawCross(i, j);
				

			if (player[i][j].noughtpos)
				DrawNought(i, j);
			

		}
	}		
			
}

void DrawCross(int x, int y)
{
	x = (x * 93) + 181;
	y = (y * 70) + 136;
	blit(cross, buffer, 0, 0, x, y, 90, 69);
}

void DrawNought(int a, int b)
{
	a = (a * 93) + 181;
	b = (b * 70) + 136;
	blit(nought, buffer, 0, 0, a, b, 90, 69);
}

void ClearArray()
{
	for (int i = 0; i<3; i++)
	{
		for (int j = 0; j<3; j++)
		{
			player[i][j].crosspos = FALSE;
			player[i][j].noughtpos = FALSE;
		}
	}
}
  
Thanks for your time. Edited by - -FlorianapoliS- on March 23, 2002 11:29:05 PM

Share this post


Link to post
Share on other sites
You can make a solutions array with every possible solution before the game begins. And check it against what''s laid out on the board.. or you are going to have do it by scanning the whole board and look for the three in a row.

Share this post


Link to post
Share on other sites