// Stephen Bassoli // CSE 124 Section 1 // Lab 2 #include #include "Board.h" #include "MyRand.h" #include "Ship.h" Board::Board() { int i, j; for (i = 0 ; i < 12 ; i++) { for (j = 0 ; j < 12 ; j++) { Grid[i][j].ship = false; Grid[i][j].hit = false; } } ShipsLeft = 0; } void Board::Place(int three, int four, int five) { int i, j, k, NumberR, NumberC; int l = 0; long errorlog; int index; int VertHorz; bool Repeat = true; for (i = 0; i < three; i++) { Ships[l].ShipThree(); l++; } for (j = 0; j < four; j++) { Ships[l].ShipFour(); l++; } for (k = 0; k < five; k++) { Ships[l].ShipFive(); l++; } ShipsLeft = three + four + five; SeedRand(); for (j = 0 ; j < ShipsLeft ; j++) { VertHorz = RandIntRange(2); VertHorz +=1; if (VertHorz == 1) { do { NumberR = RandIntRange(12); NumberC = RandIntRange(12); while (NumberR+Ships[j].ReturnLength() > 12) { NumberR = RandIntRange(12); if (!Repeat) NumberC = RandIntRange(12); } Repeat = true; for (index = 0 ; index <= Ships[j].ReturnLength() ; index++) { errorlog++; if (Grid[NumberR+index][NumberC].ship) { Repeat = false; } if (errorlog > 2000000) { cerr << "CANNOT PLACE SHIPS, USE LESS\n"; exit(1); } } } while (!Repeat); Repeat = true; errorlog = 0; Grid[NumberR][NumberC].ship = true; Grid[NumberR][NumberC].ShipNumber = j; for (i = 1; i < Ships[j].ReturnLength(); i++) { Grid[NumberR+i][NumberC].ship = true; Grid[NumberR+i][NumberC].ShipNumber = j; } } else if (VertHorz == 2) { do { NumberR = RandIntRange(12); NumberC = RandIntRange(12); while (NumberC+Ships[j].ReturnLength() > 12) { NumberC = RandIntRange(12); if (!Repeat) NumberR = RandIntRange(12); } Repeat = true; for (index = 0 ; index <= Ships[j].ReturnLength() ; index++) { errorlog++; if (Grid[NumberR][NumberC+index].ship) { Repeat = false; } if (errorlog > 2000000) { cerr << "CANNOT PLACE SHIPS, USE LESS\n"; exit(1); } } } while (!Repeat); Repeat = true; errorlog = 0; Grid[NumberR][NumberC].ship = true; Grid[NumberR][NumberC].ShipNumber = j; for (i = 1; i < Ships[j].ReturnLength(); i++) { Grid[NumberR][NumberC+i].ship = true; Grid[NumberR][NumberC+i].ShipNumber = j; } } } } void Board::Clear() { int i, j; for (i = 0 ; i < 12 ; i++) { for (j = 0 ; j < 12 ; j++) { Grid[i][j].ship = false; Grid[i][j].hit = false; } } ShipsLeft = 0; } bool Board::Mark(int R, int C) { if (!Grid[R][C].hit) { Grid[R][C].hit = true; return true; } else return false; } bool Board::IsHit(int R, int C) { int num = 0; if (Grid[R][C].ship) { num = Grid[R][C].ShipNumber; Ships[num].Hit(); TempShip = num; return true; } else return false; } bool Board::IsSink() { if (Ships[TempShip].Sink()) { ShipsLeft--; return true; } else return false; } bool Board::IsWin() { if (ShipsLeft == 0) return true; else return false; } void Board::PrintBoard()const { char Marks[145]; int i, R, C; i = 0; int index; int j = 0; char Letter = 'A'; for (R = 0 ; R < 12 ; R++) { for (C = 0 ; C < 12 ; C++) { /*if (Grid[R][C].ship) Marks[i] = 'T'; else*/ if (!Grid[R][C].hit) Marks[i] = ' '; else if ((Grid[R][C].hit)&&(!Grid[R][C].ship)) Marks[i] = 'O'; else if ((Grid[R][C].hit)&&(Grid[R][C].ship)) Marks[i] = 'X'; i++; } } cout << endl; cout << " | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10| 11| 12| \n"; cout << "----------------------------------------------------\n"; for (index = 0 ; index < 12 ; index++) { cout << Letter << " | " << Marks[j]; j++; cout << " | " << Marks[j]; j++; cout << " | " << Marks[j]; j++; cout << " | " << Marks[j]; j++; cout << " | " << Marks[j]; j++; cout << " | " << Marks[j]; j++; cout << " | " << Marks[j]; j++; cout << " | " << Marks[j]; j++; cout << " | " << Marks[j]; j++; cout << " | " << Marks[j]; j++; cout << " | " << Marks[j]; j++; cout << " | " << Marks[j]; j++; cout << " | \n"; Letter = char(Letter + 1); } cout << endl; } int Board::ReturnShipsLeft() { return ShipsLeft; }