/Projects/BattleShips.cpp   Source Browser
// BattleShips.cpp : Defines the entry point for the console application.
// Cynthia Miller	
// C++
// 10.25.07
#include "stdafx.h"
#include <cstdlib>
#include <ctime>
#include <iostream>
using namespace std;
// Status Definitions
#define EMPTY_CELL 0
#define SHIP_CELL 1
#define HIT_SHIP 2
#define MISS_CELL 3
#define MAX_SIZE_ROWS 11
#define MAX_SIZE_COLUMNS 11
// Ships
	int battleship = 4;
	int ship = 3;
	int boat = 2;
	int raft =1;
	int i;
	int j;
	int shipCounter;
	int currentShip;
	int shipDirection;
	int x;
	int y;
	int count;
	int playerHitCounter;
	int aiHitCounter;
// Declare arrays
	int shipLength[10] = {4, 3, 3, 2, 2, 2, 1, 1, 1, 1};
	int ships_ai[MAX_SIZE_COLUMNS][MAX_SIZE_ROWS];
	int target_ai[MAX_SIZE_COLUMNS][MAX_SIZE_ROWS]; 
	int ships_player[MAX_SIZE_COLUMNS][MAX_SIZE_ROWS];
	int target_player[MAX_SIZE_COLUMNS][MAX_SIZE_ROWS];
	bool playerTurn = true;
	bool checkOk;
	bool win = false;
	bool lose = false;
	int checkX;
	int checkY;
	void board();
	void AI_place();
	void extendDir();
	void checkDirection();
	void player_place();
	void player_turn();
	void ai_turn();
	void end();
	void checkBoth();
void main()
{
	srand((unsigned)time(0));
	cout << "Battle Ship 9.4\n\n";
	cout << "Welcome to Battle Ship\n\n";
	cout << "You have 1 battleship which is 4 spaces.\n";
	cout << "You have 2 ships which are 3 spaces.\n";
	cout << "You have 3 boats which are 2 spaces.\n";
	cout << "You have 4 rafts which are 1 space.\n\n";
	cout << "You will place your ships now...\n";
	cout << "You will place your battleship first..\n";
	for (shipCounter = 0; shipCounter < 10; shipCounter++)
	{
		player_place();
		board();
	}
	AI_place();
	// 0 = vertical
	// 1 = horizontal
	while(win == false && lose == false)
	{
		player_turn();
		board();		
		ai_turn();
		board();
	}
	end();
}
void board()
{
	// Draw board
	// Board legend
	// 0 = No Ship
	// 1 = No Ship Miss
	// 2 = Ship
	// 3 = Ship Hit
// Player board
	cout << "Players Targets.\n\n";
	for (int v = 0; v < 12; v++)
	{
		for (int h = 0; h < 12; h++)
		{
			if (target_player[v][h] == 0 || target_player[v][h] == 2)
			{
				cout << "[ ]";	
			}
			else if (target_player[v][h] == 1)
			{
				cout << "[M]";
			}
			else if (target_player[v][h] == 3)
			{
				cout << "[X]";
			}
		}
		cout << "\n";
	}
	cout << "\n";
// Your ships
	cout << "Your Ships.\n\n";
	for (int v = 0; v < 12; v++)
	{
		for (int h = 0; h < 12; h++)
		{
			if (ships_player[v][h] == 0)
			{
				cout << "[ ]";	
			}
			else if (ships_player[v][h] == 2)
			{
				cout << "[S]";
			}
			else if (ships_player[v][h] == 1)
			{
				cout << "[M]";
			}
			else if (ships_player[v][h] == 3)
			{
				cout << "[X]";
			}
		}
		cout << "\n";
	}
	cout << "\n";
}
//Ai Ships placement
void AI_place()
{
	shipCounter = 0;
	playerTurn = false;
	for (i=0; i< MAX_SIZE_COLUMNS; i++)
	{
		for (j=0; j< MAX_SIZE_ROWS; j++)
		{
			ships_ai[i][j] = EMPTY_CELL;
		}
	}
	while(shipCounter < 10)
	{
		x = rand()% MAX_SIZE_COLUMNS;
		y = rand()% MAX_SIZE_ROWS;
		for (i=0; i<MAX_SIZE_COLUMNS; i++)
		{
			if (ships_ai[y+i][x] == EMPTY_CELL)
			{
				shipDirection = rand()% 2;
				checkDirection();
			}
			else
			{
				checkOk = false;
				break;
			}
		}
		if (checkOk == true)
		{
			ships_ai[y][x] = 2;			
			extendDir();
			shipCounter++;
		}
	}
}
void extendDir()
{
	if (shipDirection == 0) 
	{
		x++;
 		for (int i = 1; i < shipLength[shipCounter];i++)
		{
			if (playerTurn == true)
			{
				ships_player[y][x] = 2;
				//checkBoth();
			}
			else 
			{
				ships_ai[y][x] = 2;	
			}
			x++;
		}
	}
	else if (shipDirection == 1)
	{
		y++;
		for (int i = 1; i < shipLength[shipCounter];i++)
		{
			if (playerTurn == true)
			{
				ships_player[y][x] = 2;
				//checkBoth();
			}
			else 
			{
				ships_ai[y][x] = 2;	
			}
			y++;
		}
	}
}
void checkDirection()
{
	checkX = x;
	checkY = y;
	checkOk = true;
	if (shipDirection == 0) 
	{
		checkY++;
		for (int i = 1; i < shipLength[shipCounter];i++)
		{
			if (checkY >= 12)
			{
				checkOk = false;
			}
			else if (playerTurn == true)
			{
				if (ships_player[checkY][checkX] == 2)
				{
					checkOk = false;
				}
			}
			else 
			{
				if (ships_ai[checkY][checkX] == 2)
				{
					checkOk = false;
				}	
			}
			checkY++;
		}
	}
	else if (shipDirection == 1)
	{
		checkX++;
		for (int i = 1; i < shipLength[shipCounter];i++)
		{
			if (checkX >= 12)
			{
				checkOk = false;
			}
			else if (playerTurn == true)
			{
				if (ships_player[checkY][checkX] == 2)
				{
					checkOk = false;
				}
			}
			else 
			{
				if (ships_ai[checkY][checkX] == 2)
				{
					checkOk = false;
				}	
			}
			checkX++;
		}
	}
}
void checkBoth()
{
	cout << "checking ai's ship placement\n\n";
	for (int v = 0; v < 12; v++)
	{
		for (int h = 0; h < 12; h++)
		{
			if (ships_ai[v][h] == 0)  
			{
				cout << "[ ]";	
			}
			else if (ships_ai[v][h] == 2)
			{
				cout << "[T]";
			}
			else if (ships_ai[v][h] == 1)
			{
				cout << "[M]";
			}
			else if (ships_ai[v][h] == 3)
			{
				cout << "[X]";
			}
		}
		cout << "\n";
	}
	cout << "\n";
	cout << "checking players's ship placement\n\n";
	for (int v = 0; v < 12; v++)
	{
		for (int h = 0; h < 12; h++)
		{
			if (ships_player[v][h] == 0)
			{
				cout << "[ ]";
			}
			else if (ships_player[v][h] == 2)
			{
				cout << "[M]";
			}
			else if (ships_player[v][h] == 1)
			{
				cout << "[M]";
			}
			else if (ships_player[v][h] == 3)
			{
				cout << "[X]";
			}
		}
		cout << "\n";
	}
	cout << "\n";
	// end
}
void player_place()
{
	checkOk = false;
	while (!checkOk)
	{
		cout << "What column shall you're ship start at?\n";
		cin >> x;
		cout << "What row shall you're ship start at?\n";
		cin >> y;
		if (ships_player[y][x] == 2)
		{
			checkOk = false;
		}
		else
		{
			cout << "Shall the ship be horizontal(0) or vertical(1)? 0/1?\n";
			cin >> shipDirection;
			checkDirection();
		}
		if (!checkOk)
		{
			cout << "Nope that spot does not work, try again.\n";
		}
	}
	ships_player[y][x] = 2;
	extendDir();	
	}
void player_turn()
{
	checkOk = false;
	while (!checkOk)
	{
		cout << "What column would you like to launch a missle at?\n";
		cin >> x;
		cout << "What row would you like to launch a missle at?\n";
		cin >> y;
		if (target_player[y][x] == 1 || target_player[y][x] == 3)
		{
			checkOk = false;
			cout << "Nope that spot is taken already, try again.\n";
		}
		else
			checkOk = true;
	}
	if (ships_ai[y][x] == 0)
	{
		target_player[y][x] = 1;
	}
	else if (ships_ai[y][x] == 2)
	{
		target_player[y][x] = 3;
		checkDirection();
		playerHitCounter++;
		if (playerHitCounter == 20)
		{
			win = true;
		}
	}
}
// AI turn
void ai_turn()
{ // working fine 11.20.07
	checkOk = false;
	while (!checkOk)
	{
		y = rand()%MAX_SIZE_COLUMNS;
		x = rand()%MAX_SIZE_ROWS;
		if (ships_player[y][x] == 1 || ships_player[y][x] == 3)
			{
				checkOk = false;
			}
			else
				checkOk = true;
	}
	if (ships_player[y][x] == 0)
	{
		ships_player[y][x] = 1;
	}
	else if (ships_player[y][x] == 2)
	{
		ships_player[y][x] = 3;
		aiHitCounter++;
		if (aiHitCounter == 20)
		{
			lose = true;
		}
	}
}
void end()
{ // working fine 11.20.07
	if (playerHitCounter == 20)
	{
		cout << "Congrats! You won! wooopie!\n\n";
		system ("pause");
	}
	else
	{
		cout << "Sorry you lose.\n\n";
		system ("pause");
	}
}
</body>
</html>