E-mail: magikgod@mail.ru

Игра Морской Бой часть 5
void play(int flag);
}


#endif




//////////////////////////////////////////////////////////
//game.cpp
//////////////////////////////////////////////////////////


#include <iostream.h>
#include <windows.h>

#include <conio.h>
#include "field.h"
#include "utils.h"
#include "ships.h"
#include "gameplace.h"
#include "computer.h"
#include "game.h"


using namespace MyField;
using namespace MyUtils;
using namespace MyShips;
using namespace MyComputer;
using namespace GamePlace;
using namespace MyGame;

//предварительные установки
void MyGame :: preset(GAMEPLACE &gameplace, int flag)
{
//расставить корабли
if(flag == FORPLAYER)
setAllShips(gameplace);
else
{
char ch;
cout << "Do you want to set ships with keyboard? (y/n): ";
cin >> ch;
if(ch == ''y'')
{
COORD coord = {5, 5};
clearField(gameplace.field);
fill();
setShipsWithKeyboard(gameplace, coord);
}
else
setAllShips(gameplace);
}

//установить нужный режим показа кораблей
if(flag == FORPLAYER)
setShowMode(gameplace.field, NOSHOWSHIPS);
else
setShowMode(gameplace.field, SHOWSHIPS);

}

//функция принимает в качестве параметра режим игры
//(компьютер с компьютером, человек с компьютером,
//человек с человеком) и запускает цикл игры
void MyGame :: play (int flag)
{
COORD player1Coord = {5, 5}, player2Coord = {40, 5};
GAMEPLACE player1Gameplace, player2Gameplace;

//указатели на функции
void (*player1Move)(GAMEPLACE &gameplace, COORD player1Coord);
void (*player2Move)(GAMEPLACE &gameplace, COORD player2Coord);


//присвоить указателям функции соответствующие
//данному режиму игры
if(flag == PLAYERVSCOMP)
{
player1Move = playerMove;
player2Move = computerMove;
preset(player1Gameplace, FORCOMP);
preset(player2Gameplace, FORPLAYER);
}
else
if(flag == COMPVSPLAYER)
{
player1Move = computerMove;
player2Move = playerMove;
preset(player1Gameplace, FORPLAYER);
preset(player2Gameplace, FORCOMP);
}

fill();
WriteText("PLAYER1", 7, 10, 3);
WriteText("PLAYER2", 7, 45, 3);
drawField(player1Gameplace.field, player1Coord);
drawField(player2Gameplace.field, player2Coord);


//игроки ходят, пока кто-либо не проиграет
while((!LOST(player1Gameplace))&&(!LOST(player2Gameplace)))
{

player1Move(player2Gameplace, player2Coord);
drawField(player2Gameplace.field, player2Coord);

if(!LOST(player2Gameplace))
{
player2Move(player1Gameplace, player1Coord);
drawField(player1Gameplace.field, player1Coord);
}
}

drawField(player1Gameplace.field, player1Coord);
drawField(player2Gameplace.field, player2Coord);
if(LOST(player1Gameplace))
WriteText("PLAYER2 WON!!!", 14, 30, 20);
else
WriteText("PLAYER1 WON!!!", 14, 30, 20);

char ch = 0;

while((ch = getch()) != 13);
}

Hosted by uCoz