6 Replies - 309 Views - Last Post: 12 December 2011 - 01:01 PM Rate Topic: -----

#1 Jon.Reed  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 02-June 09

Error: '♣' expected

Posted 12 December 2011 - 11:13 AM

I need help trying to figure out why my checkDist(...) function is returning an error of '♣' expected when I compile.

I have searched google, but the error is just either too broad, or too narrow for anything actually helpful to appear(that or my search criteria was not good).

If someone could either point me in the right direction about what is causing the error, or help me fix it I would be extremely thankful.

The error:
Main2.c:414: '♣' expected

#include "Battleship.h"
#include "Menu.c"

typedef struct
{
	int x;
	int y;
	char dir;
	int len;
} SHIP;

typedef struct
{
	int isPlayer;
	int hide;
	char top[10][10]; //Enemy ocean
	char bot[10][10]; //You ocean
	//Keep track of which ships have been placed.
	int count;
	SHIP pieces[5];
} PLAYER;

void setupBoard(PLAYER* a);
void placeShip(PLAYER* a, int shipSize);
int checkDist(char board[][10], int x, int y, char dir, int len);
int checkDistComplex(char board[][10], int x, int y, int xD, int yD, int len);
void turn(PLAYER* a, PLAYER* B)/>;
void printGameScreen(char top[][10],char bot[][10], int hide);
void printBoard(char board[][10], int hide);
int stillAlive(PLAYER* a);
void resetBoard(char board[][10]);

int main(void)
{
	PLAYER one = {1, 0, {'~'}, {'~'}, 0, {0,0,'U',0,10}};
	PLAYER two = {1, 0, {'~'}, {'~'}, 0, {0,0,'U',0,10}};
	
	srand(time(NULL));
	int mChoice, dChoice = 0;
	do
	{
		resetBoard(one.bot);
		resetBoard(one.top);
		resetBoard(two.bot);
		resetBoard(two.top);
		mChoice = MainMenu();
		if(mChoice != 4)
		{
			switch(mChoice)
			{
			case 1:
				one.isPlayer = 1;
				one.hide = 0;
				two.isPlayer = 1;
				two.hide = 0;
				system("Cls");
				printf("Player 1, please setup up your board.\n");
				system("Pause");
				setupBoard(&one);
				system("Cls");
				printf("Player 2, please setup up your board.\n");
				system("Pause");
				setupBoard(&two);
				break;
			case 2:
				one.isPlayer = 1;
				one.hide = 0;
				two.isPlayer = 0;
				two.hide = 1;
				dChoice = DiffMenu();
				system("Cls");
				printf("Player 1, please setup up your board.\n");
				system("Pause");
				setupBoard(&one);
				system("Cls");
				printf("Computer 2 is setting up his board.\n");
				system("Pause");
				setupBoard(&two);
				break;
			case 3:
				one.isPlayer = 0;
				one.hide = 0;
				two.isPlayer = 0;
				two.hide = 0;
				dChoice = DiffMenu();
				system("Cls");
				printf("Computer 2 is setting up his board.\n");
				system("Pause");
				setupBoard(&one);
				system("Cls");
				printf("Computer 2 is setting up his board.\n");
				system("Pause");
				setupBoard(&two);
				break;
			}	
		}
		if(dChoice != 4)
		{
			int counter = 0;
			do
			{
				if(counter%2 == 0)
				{
					turn(&one,&two);
				}
				else
				{
					turn(&two,&one);
				}
				if(two.isPlayer == one.isPlayer)
				{
					if(two.isPlayer == 1)
					{
						printf("Giving other player time to look away. Ready?");
					}
					else
					{
						printf("Next turn for bots.");
					}
					system("Pause");
				}
				counter++;
				if(counter%10) {printf("Ctrl+C at any time will close the program.");}
			}
			while(stillAlive(&one) && stillAlive(&two));
		}
	}
	while(mChoice != 4);
	
	system("Pause");
	return 0;
}

int stillAlive(PLAYER* a)
{
	int j, k;
	int alive = 0;
	for(j = 0; j < 5; j++)
	{
		SHIP temp = a->pieces[j];
		switch(tolower(temp.dir))
		{
		case 'u':
			for(k = 0; k < temp.len; k++)
			{
				if(a->bot[temp.x][temp.y-k] == 'O')
				{
					return 1;
				}
			}
			break;
		case 'd':
			for(k = 0; k < temp.len; k++)
			{
				if(a->bot[temp.x][temp.y+k] == 'O')
				{
					return 1;
				}
			}
			break;
		case 'l':
			for(k = 0; k < temp.len; k++)
			{
				if(a->bot[temp.x-k][temp.y] == 'O')
				{
					return 1;
				}
			}
			break;
		case 'r':
			for(k = 0; k < temp.len; k++)
			{
				if(a->bot[temp.x+k][temp.y-k] == 'O')
				{
					return 1;
				}
			}
			break;
		}
	}
	return 0;
}
		

void setupBoard(PLAYER* a)
{
	if(a->isPlayer)
	{
		placeShip(a,5);
		placeShip(a,4);
		placeShip(a,3);
		placeShip(a,3);
		placeShip(a,2);
	}
}
	

void resetBoard(char board[][10])
{
	int k, j;
	for(k = 0; k < 10; k++)
	{
		for(j = 0; j < 10; j++)
		{
			board[k][j] = '~';
		}
	}
}
void printBoard(char board[][10], int hide)
{
	if(DEBUG)
	{
	printf("         11111111112222222222333333333344444444445555555555\n");
	printf("12345678901234567890123456789012345678901234567890123456789\n");
	}
	printf("#################### 0 1 2 3 4 5 6 7 8 9 ###################\n");
	int k, j;
	for(k = 0; k < 10; k++) //Rows : Y
	{
		printf("################## %d ",k);
		for(j = 0; j < 10; j++) //Colunms : X
		{
			if(hide)
			{
				printf("~ ");
			}
			else
			{
				printf("%c ",board[j][k]);
			}
		}
		printf("####################\n");
	}
}

void printGameScreen(char top[][10], char bot[][10], int hide)
{
	if(DEBUG)
	{
	printf("         11111111112222222222333333333344444444445555555555\n");
	printf("12345678901234567890123456789012345678901234567890123456789\n");
	}
	printf("################B  A  T  T  L  E   S  H  I  P###############\n");
	printf("____________________________________________________________\n");
	printf("####################E N E M Y   O C E A N###################\n");
	printBoard(top,hide);
	printf("____________________________________________________________\n");
	printf("#####################Y O U R   O C E A N####################\n");
	printBoard(bot,hide);
}

void turn(PLAYER* a, PLAYER* B)/>
{
	int tempx, tempy, ioResult;
	do
	{
		system("Cls");
		printGameScreen(a->top,a->bot,a->hide);
		if(a->isPlayer)
		{
			printf("Enter target coordinates: X Y\n");
			ioResult = scanf("%d %d", &tempx, &tempy);
			FLUSH;
			if(ioResult < 2)
			{
				printf("Input should look like this( [] designates options ):  [0-9] [0-9]\n");
				system("Pause");
			}
			else if(!(tempx >= 0 && tempx < 10) || !(tempy >= 0 && tempy < 10))
			{
				printf("X and Y must both be a number from 0 to 9.\n");
				system("Pause");
				ioResult = -1;
			}
			else if(a->top[tempx][tempy] != '~')
			{
				printf("Targeted spot has already been checked.\n");
				system("Pause");
				ioResult = -1;
			}
		}
		else
		{
			printf("Targeting....\n");
			tempx = rand()%10;
			tempy = rand()%10;
			if(a->top[tempx][tempy] != '~')
			{
				printf("Targeting...\n");
				ioResult = -1;
			}
		}
	}
	while(ioResult < 2);
	if(b->bot[tempx][tempy] == 'O')
	{
		printf("You hit an enemy ship!");
		a->top[tempx][tempy] = 'X';
		b->bot[tempx][tempy] = 'X';
	}
	else if(b->bot[tempx][tempy] == '~')
	{
		printf("There was nothing in the area.");
		a->top[tempx][tempy] = ' ';
		b->bot[tempx][tempy] = ' ';
	}
}

void placeShip(PLAYER* a, int shipSize)
{
	int tempx, tempy, ioResult;
	char tempdir;
	//Place ship.
	do
	{
		system("Cls");
		printBoard(a->bot, a->hide);
		if(a->isPlayer)
		{
			printf("Enter X Y pos of your battleship followed by direction(U D L R): X Y \n");
			ioResult = scanf("%d %d %c", &tempx, &tempy, &tempdir);
			FLUSH;
			if(ioResult < 3)
			{
				printf("Input should look like this( [] designates options ):  [0-9] [0-9] [U D L R]\n");
				system("Pause");
			}
			else if(!(tempx >= 0 && tempx < 10) || !(tempy >= 0 && tempy < 10) || strchr("udlr",tolower(tempdir)) == NULL)
			{
				printf("X and Y must both be a number from 0 to 9. Direction must be either u, d, l, or r.\n");
				system("Pause");
				ioResult = -1;
			}
			else if(checkDist(a->bot,tempx,tempy,tempdir, shipSize) == 0)
			{
				printf("The direction selected causes 1 or more sections of the ship to collide with another ship.\n");
				system("Pause");
				ioResult = -1;
			}
			else if(checkDist(a->bot,tempx,tempy,tempdir, shipSize) == -1)
			{
				printf("The direction selected causes 1 or more sections of the ship to leave the board.\n");
				system("Pause");
				ioResult = -1;
			}
		}
		else
		{
			printf("Placing ship... \n");
			tempx = rand()%10;
			tempy = rand()%10;
			switch(rand()%4)
			{
				case 0:
					tempdir = 'u';
					break;
				case 1:
					tempdir = 'l';
					break;
				case 2:
					tempdir = 'r';
					break;
				case 3:
					tempdir = 'd';
					break;
			}
			if(checkDist(a->bot,tempx,tempy,tempdir, shipSize) == 0)
			{
				printf("Placing Ship....\n");
				ioResult = -1;
			}
			else if(checkDist(a->bot,tempx,tempy,tempdir, shipSize) == -1)
			{
				printf("Placing Ship..\n");
				ioResult = -1;
			}
	}
	while(ioResult < 3);
	SHIP temp = {tempx, tempy, tempdir, shipSize};
	a->pieces[a->count] = temp;
	a->count += 1;

	int k;
	switch(tolower(tempdir))
	{
	case 'u':
		
		for(k = 0; k < shipSize; k++)
		{
			a->bot[tempx][tempy-k] = 'O';
		}
		break;
	case 'd':
		for(k = 0; k < shipSize; k++)
		{
			a->bot[tempx][tempy+k] = 'O';
		}
		break;
	case 'l':
		for(k = 0; k < shipSize; k++)
		{
			a->bot[tempx-k][tempy] = 'O';
		}
		break;
	case 'r':
		for(k = 0; k < shipSize; k++)
		{
			a->bot[tempx+k][tempy] = 'O';
		}
		break;
	}
}

int checkDist(char board[][10], int x, int y, char dir, int len)
{
	int xD, yD;
	switch(tolower(dir))
	{
	case 'l':
			xD = -1; 
			yD = 0;
		break;
	case 'r':
			xD = 1; 
			yD = 0;
		break;
	case 'u':
			xD = 0; 
			yD = -1;
		break;
	case 'd':
			xD = 0; 
			yD = 1;
		break;
	}
	if(x >= 0 && x < 10 && y >= 0 && y < 10)
	{
		if(len > 1 && board[x][y] == '~')
		{
			return checkDistComplex(board, x+xD, y+yD, xD, yD, len-1);
		}
		else if ( len == 1 && board[x][y] == '~')
		{
			return 1;
		}
		else
		{
			return 0;
		}
	}
	else
	{
		return -1;
	}
}

int checkDistComplex(char board[][10], int x, int y, int xD, int yD, int len)
{
	if(x >= 0 && x < 10 && y >= 0 && y < 10)
	{
		if(len > 1 && board[x][y] == '~')
		{
			return checkDistComplex(board, x+xD, y+yD, xD, yD, len-1);
		}
		else if ( len == 1 && board[x][y] == '~')
		{
			return 1;
		}
		else
		{
			return 0;
		}
	}
	else
	{
		return -1;
	}
}



Is This A Good Question/Topic? 0
  • +

Replies To: Error: '♣' expected

#2 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 8962
  • View blog
  • Posts: 33,586
  • Joined: 12-June 08

Re: Error: '♣' expected

Posted 12 December 2011 - 11:16 AM

Throw a try/catch around the call to that method (or just inside it)... and see if it gives a more verbose exception!
Was This Post Helpful? 0
  • +
  • -

#3 Jon.Reed  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 02-June 09

Re: Error: '♣' expected

Posted 12 December 2011 - 11:55 AM

I completely forgot to mention that this is in C and I used tcc to compile. How do I do a try {} catch {} in C? We did not go over that this semester.
Was This Post Helpful? 0
  • +
  • -

#4 RetardedGenius  Icon User is offline

  • >>──(Knee)──►
  • member icon

Reputation: 126
  • View blog
  • Posts: 555
  • Joined: 30-October 10

Re: Error: '♣' expected

Posted 12 December 2011 - 12:04 PM

C doesn't have that functionality, it's pretty bare-bones. If you want a try-catch statement you'll have to turn to C++.

For exception handling in C (not C++), I'd recommend giving this a butcher's. :)
Was This Post Helpful? 0
  • +
  • -

#5 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2091
  • View blog
  • Posts: 3,185
  • Joined: 21-June 11

Re: Error: '♣' expected

Posted 12 December 2011 - 12:15 PM

Note that even if C did have try-catch it would do very little good here, because putting try catch into your code wouldn't possibly change compile error messages (unless of course you introduced new errors while adding the try-catch).
Was This Post Helpful? 1
  • +
  • -

#6 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2091
  • View blog
  • Posts: 3,185
  • Joined: 21-June 11

Re: Error: '♣' expected

Posted 12 December 2011 - 12:27 PM

If I try to compile this using gcc (and fix or ignore all the errors that are caused by me not having the header-files you're including) I get a more helpful error message:

bla.c:416:1: Error: expected »while« before »int«

So it looks like gcc thinks the closing brace on line 414 closes a do-loop, and not the definition of placeShip. And sure enough if I use my trusty emacs to properly indent the source file, it turns out the braces aren't well-matched, so you forgot a closing brace somewhere.

The lesson you should learn from that is that you should use an editor which automatically and properly indents your source code, so you immediately know when you're missing braces somewhere. Also you might want to use a compiler whose error messages contain more information than what the compiler-writer's favorite suite of cards is.
Was This Post Helpful? 2
  • +
  • -

#7 Jon.Reed  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 02-June 09

Re: Error: '♣' expected

Posted 12 December 2011 - 01:01 PM

Ah, I knew it was most likely a stupid mistake. Thank you for the all the help. Now to check if it runs.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1