#include #include #include "BoardT.h" #include "CoordT.h" #include "SquareT.h" #include #include using namespace std; void DrawBoard(const BoardT & board); vector MakeDeck(size_t size); void ShuffelDeck(vector & deck); void FillBoard(BoardT & board); void PlayGame(BoardT & board); int main() { BoardT board(4,5); PlayGame(board); return 0; } CoordT GetCoord(const BoardT & board){ CoordT coord{board.NewCoord()}; size_t x, y; bool first{true}; do { if (first) { first = false; } else { cout << " That choice is not valid" << endl; } cout << "Enter a coordinate" << endl; cout << " x: " ; cin >> x; cout << " y: "; cin >> y; coord.Set(x,y); } while (not coord.SuccessfulChange() or not board[coord].HasCard() or board[coord].IsFaceUp()); return coord; } void PrintCard(CardT card) { cout << "\tYou Picked the " ; cout << card.Value() << " of " << SuiteTToString(card.Suite()) << "s"<< endl; } void PlayGame(BoardT & board){ size_t cards{board.Width()*board.Height()}; CoordT coord1{board.NewCoord()}; CoordT coord2{board.NewCoord()}; FillBoard(board); while (cards > 0) { cout << "NEW TURN " << endl; DrawBoard(board); cout << endl << endl; coord1 = GetCoord(board); board[coord1].Flip(); PrintCard(board[coord1].GetCard()); DrawBoard(board); coord2 = GetCoord(board); board[coord2].Flip(); PrintCard(board[coord2].GetCard()); DrawBoard(board); if (board[coord1].GetCard() == board[coord2].GetCard()) { cout << "Match " << endl << endl; board[coord1].RemoveCard(); board[coord2].RemoveCard(); cards -= 2; } else { cout << "No Match" << endl << endl; board[coord1].Flip(); board[coord2].Flip(); } cout << endl << endl; } } void FillBoard(BoardT & board){ vectordeck{MakeDeck(board.Width()*board.Height())}; size_t x; size_t y; int i{0}; CoordT coord{board.NewCoord()}; ShuffelDeck(deck); for(x = 0; x < board.Width(); x++) { for (y =0; y < board.Height(); y++) { coord.Set(x,y); board[coord].SetCard( deck[i]); i++; } } } void ShuffelDeck(vector & deck){ size_t i, target; for(i =0; i < deck.size(); i++) { target = rand() % deck.size(); if (target != i) { // swap in in algorithm swap(deck[i], deck[target]); } } } vector MakeDeck(size_t size){ vector deck; CardT card; for(size_t i=0; i < size/2; i++) { card.Random(); deck.push_back(card); deck.push_back(card); } return deck; } void Line(size_t width){ size_t x; cout << " +"; for(x = 0; x < width; x++){ cout << "--+"; } cout << endl; } void DrawBoard(const BoardT & board){ CoordT coord{board.NewCoord()}; SquareT space; size_t x,y; cout << " "; for(x = 0; x < board.Width(); x++) { cout << setw(3) << x ; } cout << endl; Line(board.Width()); for (y = 0; y < board.Height(); y++){ cout << setw(3) << y << "|"; for(x = 0; x < board.Width(); x++) { coord.Set(x,y); space = board[coord]; if (space.HasCard()) { if (space.IsFaceUp()) { cout << space.GetCard().Value(); cout << SuiteTToString(space.GetCard().Suite())[0]; } else { cout << "XX"; } } else { cout << " "; } cout << "|"; } cout << endl; Line(board.Width()); } return ; }