implementing a scrabble in c 2b 2b

Solutions on MaxInterview for implementing a scrabble in c 2b 2b by the best coders in the world

showing results for - "implementing a scrabble in c 2b 2b"
Aqua
30 Apr 2019
1//board.cpp
2#include "board.h"
3 
4CBoard::CBoard()
5{
6xmax = ymax = 15;
7firstword = FALSE;
8//create the initial tile set and put them in the tilebag list
9char tile_letters[26] = {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};
10int numtiles[26] = {9, 2, 2, 4, 12, 2, 3, 2, 9, 1, 1, 4, 2, 6, 8, 2, 1, 6, 4, 6, 4, 2, 2, 1, 2, 1};
11 
12int i; //outer loop counter
13int j; //inner loop counter
14for(i=0;i<26;i++) { //for each letter
15for(j=0;j<numtiles[i];j++) { //make tile j times
16tilebag.AddHead(new CTile(tile_letters[i]));
17}
18}
19//special case for blanks
20//makes 2 blank tiles
21tilebag.AddHead(new CTile());
22tilebag.AddHead(new CTile());
23 
24//set the squares array to NULL
25for (i=0;i<15;i++) {
26for (j=0;j<15;j++) {
27squares[i][j] = NULL;
28}
29}
30 
31}
32//returns NULL if there are no tiles left in bag
33CObject* CBoard::GetRandomTile() {
34CObject *tmp;
35POSITION pos;
36srand(time(NULL));//used to when getting a random number between 0 and number elements in tilebag
37int limit = tilebag.GetCount();
38//limit++; //evil bug of random crashies = squashed!
39int random;
40//special cases where random breaks
41if (limit == 0) //fix
42return NULL;
43 
44if (limit == 1) {
45pos = tilebag.FindIndex(0);
46tmp = tilebag.GetAt(pos);
47tilebag.RemoveAt(pos);
48return tmp;
49}
50//////////////////////////////////
51 
52random = rand()%limit;
53//the general random workies case
54if( ( pos = tilebag.FindIndex( random )) != NULL ) {
55 
56tmp = tilebag.GetAt(pos);
57tilebag.RemoveAt(pos);
58} 
59else {
60tmp = NULL;
61}
62 
63return tmp;
64}
65//returns a copy of the tile at specified position (used in drawing)
66CObject* CBoard::GetBoardTile(int x, int y) {
67return squares[x][y];
68}
69///////////////////////////
70// polymorphism at work :) 
71//////////////////////////
72//return a tile to the tilebag (swapping)
73void CBoard::AddTile(CObject *tile) {
74tilebag.AddTail(tile);
75}
76//add tile to a square[x][y]
77void CBoard::AddTile(CObject *tile, int x, int y) {
78 
79squares[x][y] = tile;
80}
81///////////////////////////
82// checks a squares index position to see if tile is there
83bool CBoard::IsEmpty(int x, int y) {
84 
85//make sure player score functions dont request tiles out of board
86if (x < 0 || x >= xmax || y < 0 || y >= ymax)
87return TRUE;
88 
89if (squares[x][y] == NULL) {
90return TRUE;
91}
92else {
93return FALSE;
94}
95}
96//returns value of tile at position
97int CBoard::GetScore(int x, int y) {
98CObject *object;
99CTile *tile;
100//make sure player score functions dont request tiles out of board
101if (x < 0 || x >= xmax || y < 0 || y >= ymax)
102return 0;
103if (squares[x][y] == NULL) {
104return 0;
105}
106else {
107//read out tile->value
108object = squares[x][y];
109tile = (CTile *)object;
110return tile->GetValue();
111}
112 
113}
114CBoard::~CBoard()
115{
116//traverse squares and tilebag to delete tiles
117CObject *object;
118CTile *tile;
119POSITION pos;
120int i,j;
121//remove tiles in the tilebag
122for( pos = tilebag.GetHeadPosition(); pos != NULL; ) { 
123 
124object = tilebag.GetNext(pos);
125tile = (CTile*) object;
126 
127delete tile;
128}
129//remove tiles in squares[][]
130for (i=0;i<15;i++) {
131for (j=0;j<15;j++) {
132object = squares[i][j] = NULL;
133tile = (CTile*) object;
134delete tile; 
135}
136}
137 
138}
139#ifndef BOARD_HEADER
140#define BOARD_HEADER
141#include "tile.h"
142#include <afxcoll.h>
143 
144class CBoard {
145public: 
146 
147bool firstword; // flag to give first player double score 
148 
149CBoard();
150CObject* GetRandomTile(); //from tilebag
151CObject* GetBoardTile(int x, int y); //returns copy of a tile at position (for drawing)
152void AddTile(CObject *tile); // add tile to tilebag (swaps)
153void AddTile(CObject *tile, int x, int y); // add tile to squares
154 
155bool IsEmpty(int x, int y); // check if squares[x][y] exists
156int GetScore(int x, int y); //checks squares position for tile and returns score or 0 if no tile
157 
158~CBoard ();
159private:
160 
161int xmax;
162int ymax; 
163 
164/* 
165Why 2D array instead of CObList, you might ask. Here are some reasons
166 
167tiles dont have to store x,y
168you dont have to traverse entire list to check if a given x,y is empty
169it is a more logical representation of real life board data
170*/ 
171CObject *squares[15][15];
172CObList tilebag; 
173 
174};
175 
176#endif // BOARD_HEADER
similar questions
queries leading to this page
implementing a scrabble in c 2b 2b