PageRenderTime 109ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/testGame.c

https://bitbucket.org/ashaw/cs1917-project-mon09spoons1
C | 1046 lines | 950 code | 48 blank | 48 comment | 202 complexity | 614b54f444b2bc9df29e7dee8f4d2546 MD5 | raw file
  1. // alexis.shaw, edward.ong, roseline.tayeh, yun.xu (Peter)
  2. // Mon09Spoons, Aleksander Budzynowski
  3. // 6/5/2011
  4. // testGame.c - tests a Game struct implementation.
  5. #include <stdlib.h>
  6. #include <stdio.h>
  7. #include <assert.h>
  8. #include <string.h>
  9. #include <math.h>
  10. #include "Game.h"
  11. //Edward's #defines
  12. #define NUM_STUDENT_TYPES 6
  13. #define NO_OF_INITIAL_ARCS 2
  14. #define NO_OF_INITIAL_GO8s 0
  15. #define NO_OF_INITIAL_CAMPUSES 2
  16. #define NO_OF_INITIAL_IPs 0
  17. #define NO_OF_INITIAL_PUBs 0
  18. //Peter's #defines
  19. #define NUM_STUDENT_TYPES 6
  20. #define NUM_OF_EACH_TYPE_STUDENT 5
  21. #define NUM_VERTICIES 54
  22. #define NUM_EDGES 72
  23. //Edward's #defines
  24. #define NUM_STUDENT_TYPES 6
  25. #define NO_OF_INITIAL_ARCS 2
  26. #define NO_OF_INITIAL_GO8s 0
  27. #define NO_OF_INITIAL_CAMPUSES 2
  28. #define NO_OF_INITIAL_IPs 0
  29. #define NO_OF_INITIAL_PUBs 0
  30. //Peter's #defines
  31. #define NUM_STUDENT_TYPES 6
  32. #define NUM_OF_EACH_TYPE_STUDENT 5
  33. #define NUM_EDGES 72
  34. //alexis shaw's typedefs
  35. typedef struct _point{
  36. int x;
  37. int y;
  38. }point;
  39. typedef struct _edge{
  40. point p1;
  41. point p2;
  42. }edge;
  43. static void testNewGame (void);
  44. static void testDisposeGame(void);
  45. static void testGettersReturnDefaults(Game g);
  46. static void testGetCampusIsDefault(Game g);
  47. static void testGetArcIsDefault(Game g);
  48. //alexis Shaw's declarations for helper functions.
  49. static void getPathThroughEdge(point start,point back,edge end,path destination);
  50. static int getVertexIndex(int specialPoint);
  51. static void getPathToVertex(int index, int type, path path);
  52. static int isOnBoard(int row,int col);
  53. static int isVertex(int row,int col);
  54. static int getArrayOfVertecies(point points[]);
  55. static double getDistance(point p1, point point2);
  56. static void getPath(point start,point back,point end,path path);
  57. static point getRight(point input,point back);
  58. static point getLeft(point input,point back);
  59. static void getSurroundingVerticies(point p1, point surroundingVertecies[]);
  60. static int getArrayOfEdges(edge edges[]);
  61. static void getPathToEdge(int index, int type, path path);
  62. static int getEdgeIndex(int specialPoint);
  63. static Game getGameWithStudents(int p1[], int p2[], int p3[],int activePlayer,int nullRoll);
  64. static Game getGameWithStudentsSmall(int studentType, int Player, int amount,
  65. int null_roll);
  66. //Edward's function declarations
  67. static void givePlayersStudents(int p1[], int p2[], int p3[], int num);
  68. static void testMakeAction(void);
  69. // under here are the sub-tests that testMakeAction calls.
  70. static void testActionBuildGO8(Game g, int activePlayer);
  71. static void testActionObtainARC(Game g, int activePlayer);
  72. static void testActionBuildCampus(Game g, int activePlayer);
  73. static void testActionObtainPublication(Game g, int activePlayer);
  74. static void testActionObtainIPPatent(Game g, int activePlayer);
  75. static void testActionRetrainStudents(Game g, int activePlayer, int disciplineFrom, int disciplineTo);
  76. static void testGetKPIPoints(void);
  77. static void testThrowDice (void);
  78. static void testGetStudents(void);
  79. int main(int argc, char* argv[]){
  80. printf("\nNow testing newGame()\n");
  81. testNewGame ();
  82. printf("\nNow testing disposeGame()\n");
  83. testDisposeGame();
  84. testMakeAction();
  85. printf("\nNow testing getKPIpoints()\n");
  86. testGetKPIPoints();
  87. printf("tests passed");
  88. printf("\nNow testing throwDice()\n");
  89. testThrowDice ();
  90. printf("tests passed");
  91. printf("\nNow testing getStudents()\n");
  92. testGetStudents();
  93. printf("tests passed");
  94. printf("\nAll tests passed! You are Awsome!\n");
  95. return EXIT_SUCCESS;
  96. }
  97. static void testGetStudents(void){
  98. Game g;
  99. int p1[NUM_STUDENT_TYPES];
  100. int p2[NUM_STUDENT_TYPES];
  101. int p3[NUM_STUDENT_TYPES];
  102. int null_roll = 0;
  103. int activePlayer = UNI_A;
  104. int noOfStudents = 0;
  105. int disc = 0;
  106. // test for all possible permutations of activePlayer and disc
  107. // (student discipline) upto a number of students defined
  108. // as 20.
  109. // at noOfStudents = 0, test is equivalent to initialisation test.
  110. while (noOfStudents < 30){
  111. activePlayer = 1;
  112. while (activePlayer < 4){
  113. givePlayersStudents(p1, p2, p3, noOfStudents);
  114. g = getGameWithStudents(p1, p2, p3, activePlayer, null_roll); //g with students
  115. disc = 1;
  116. while (disc < NUM_STUDENT_TYPES){
  117. assert(getStudents(g,activePlayer,disc) ==noOfStudents);
  118. disc++;
  119. }
  120. activePlayer++;
  121. }
  122. disposeGame(g);
  123. noOfStudents++;
  124. }
  125. //TODO: test against rollDice()
  126. }
  127. static void testThrowDice (void){
  128. int g1;
  129. int g2;
  130. int g3;
  131. Game g;
  132. int dicipline[19] = {0};
  133. int dice[19] = {2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2};
  134. g = newGame(dicipline, dice);
  135. throwDice(g, 1);
  136. g1=getTurnNumber(g);
  137. throwDice(g, 2);
  138. g2=getTurnNumber(g);
  139. assert (g2-g1==1);
  140. throwDice(g, 2);
  141. g3=getTurnNumber(g);
  142. assert (g3-g2==1);
  143. disposeGame(g);
  144. }
  145. //Roseline's test code
  146. static void testNewGame (void){
  147. Game g;
  148. int dicipline[19] = {0};
  149. int dice[19] = {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1};
  150. printf("Create a new Game g\n");
  151. g = newGame(dicipline, dice);
  152. printf("Is g NULL?\n");
  153. assert(g != NULL);
  154. printf("test that Getters return default calues on a new Game?\n");
  155. testGettersReturnDefaults(g);
  156. printf("test that The board is in a default state for a new Game\n");
  157. testGetCampusIsDefault(g);
  158. testGetArcIsDefault(g);
  159. disposeGame(g);
  160. printf("tests passed! You are Awsome!\n");
  161. }
  162. static void testDisposeGame(void){
  163. Game g;
  164. int dicipline[19] = {0};
  165. int dice[19] = {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1};
  166. printf("Create a new Game g\n");
  167. g = newGame(dicipline, dice);
  168. printf("Is g NULL?\n");
  169. assert(g != NULL);
  170. printf("does the game crash when disposeGame() is Run?\n");
  171. disposeGame(g);
  172. printf("tests passed! You are Awsome!\n");
  173. }
  174. static void testGettersReturnDefaults(Game g){
  175. int player;
  176. int discipline;
  177. int disciplineFrom;
  178. int disciplineTo;
  179. player = 1;
  180. while (player < 4){
  181. printf("\nTesting getters for player %d return Defaults\n", player);
  182. //am i meant to make the counter 'i' rather than 'player'?
  183. //if so, what do i do to the player in, say, getKPIpoints(g, player)
  184. //so that the value of player++? would i change that 'player'
  185. //to 'i' as well?
  186. printf("testing that player %d has 24 KPI points\n", player);
  187. assert (getKPIpoints(g, player)==24);
  188. printf("testing that player %d has 2 ARCs\n", player);
  189. assert (getARCs(g, player)==2);
  190. printf("testing that player %d has 0 GO8s\n", player);
  191. assert (getGO8s(g, player)==0);
  192. printf("testing that player %d has 2 Campuses\n", player);
  193. assert (getCampuses(g, player)==2);
  194. printf("testing that player %d has 0 IPs\n", player);
  195. assert (getIPs(g, player)==0);
  196. printf("testing that player %d has 0 publications\n", player);
  197. assert (getPublications(g, player)==0);
  198. printf("testing that player %d has No students of any type\n", player);
  199. for (discipline=0; discipline<=5; discipline++){
  200. assert (getStudents(g, player, discipline)==0);
  201. }
  202. printf("testing that the default exchange rate for player %d\n", player);
  203. printf("is 3 for all combinations\n");
  204. for (disciplineFrom=1; disciplineFrom<6; disciplineFrom++){
  205. for (disciplineTo=0; disciplineTo<6; disciplineTo++){
  206. if (disciplineFrom != disciplineTo){
  207. assert(getExchangeRate(g, player, disciplineFrom, disciplineTo)==3);
  208. }
  209. }
  210. }
  211. player++;
  212. }
  213. }
  214. static void testGetCampusIsDefault(Game g){
  215. int index;
  216. path pathToVertex;
  217. for (index=0; index<=53; index++){
  218. getPathToVertex(index, 0, pathToVertex);
  219. printf("we are testing vertex with path %s\n", pathToVertex);
  220. if (index==getVertexIndex(0) || index==getVertexIndex(3)){
  221. printf("campus is of type %d\n", getCampus(g, pathToVertex));
  222. printf("this is one of UNI_A's campuses, should be %d\n", CAMPUS_A);
  223. assert (getCampus(g, pathToVertex)==CAMPUS_A);
  224. } else if (index==getVertexIndex(1) || index==getVertexIndex(4)){
  225. printf("campus is of type %d\n", getCampus(g, pathToVertex));
  226. printf("this is one of UNI_B's campuses, should be %d\n", CAMPUS_B);
  227. assert (getCampus(g, pathToVertex)==CAMPUS_B);
  228. } else if (index==getVertexIndex(2) || index==getVertexIndex(5)){
  229. printf("campus is of type %d\n", getCampus(g, pathToVertex));
  230. printf("this is one of UNI_C's campuses, should be %d\n", CAMPUS_C);
  231. assert (getCampus(g, pathToVertex)==CAMPUS_C);
  232. } else{
  233. printf("campus is of type %d\n", getCampus(g, pathToVertex));
  234. printf("this is an empty Vertex, should be %d\n",VACANT_VERTEX);
  235. assert (getCampus(g, pathToVertex)==VACANT_VERTEX);
  236. }
  237. }
  238. }
  239. static void testGetArcIsDefault(Game g){
  240. int index;
  241. path pathToEdge;
  242. for (index=0; index<=71; index++){
  243. printf("we are testing vertex with path %s, index is %d\n", pathToEdge, index);
  244. getPathToEdge(index, 0, pathToEdge);
  245. if (index==getEdgeIndex(0) || index==getEdgeIndex(3)){
  246. printf("ARC is of type %d\n", getARC(g, pathToEdge));
  247. printf("this is one of UNI_A's ARCs, should be %d\n",ARC_A);
  248. assert (getARC(g, pathToEdge)==ARC_A);
  249. } else if (index==getEdgeIndex(1) || index==getEdgeIndex(4)){
  250. printf("ARC is of type %d\n", getARC(g, pathToEdge));
  251. printf("this is one of UNI_B's ARCs, should be %d\n",ARC_B);
  252. assert (getARC(g, pathToEdge)==ARC_B);
  253. } else if (index==getEdgeIndex(2) || index==getEdgeIndex(5)){
  254. printf("ARC is of type %d\n", getARC(g, pathToEdge));
  255. printf("this is one of UNI_C's ARCs, should be %d\n",ARC_C);
  256. assert (getARC(g, pathToEdge)==ARC_C);
  257. } else{
  258. assert (getARC(g, pathToEdge)==VACANT_ARC);
  259. }
  260. }
  261. }
  262. static void givePlayersStudents(int *p1, int *p2, int *p3, int num){
  263. int typeStudent = 0;
  264. while (typeStudent < NUM_STUDENT_TYPES){
  265. p1[typeStudent] = num;
  266. p2[typeStudent] = num;
  267. p3[typeStudent] = num;
  268. typeStudent++;
  269. }
  270. }
  271. static void testMakeAction(void){
  272. Game g;
  273. int p1[NUM_STUDENT_TYPES];
  274. int p2[NUM_STUDENT_TYPES];
  275. int p3[NUM_STUDENT_TYPES];
  276. int activePlayer = 1;
  277. int null_roll = 3; // ???
  278. int i, j;
  279. while (activePlayer < NUM_UNIS+1){
  280. printf("\nNow testing makeAction, for OBTAIN_ARC for player %d\n", activePlayer);
  281. givePlayersStudents(p1, p2, p3, 5);
  282. g = getGameWithStudents(p1, p2, p3, activePlayer, null_roll);
  283. testActionObtainARC(g, activePlayer);
  284. disposeGame(g);
  285. printf("Tests passed, you are Awsome!\n");
  286. printf("\nNow testing makeAction, for BUILD_CAMPUS for player %d\n", activePlayer);
  287. givePlayersStudents(p1, p2, p3, 5);
  288. g = getGameWithStudents(p1, p2, p3, activePlayer, null_roll);
  289. testActionBuildCampus(g, activePlayer);
  290. disposeGame(g);
  291. printf("Tests passed, you are Awsome!\n");
  292. printf("\nNow testing makeAction, for BUILD_GO8 for player %d\n", activePlayer);
  293. givePlayersStudents(p1, p2, p3, 5);
  294. g = getGameWithStudents(p1, p2, p3, activePlayer, null_roll);
  295. testActionBuildGO8(g, activePlayer);
  296. disposeGame(g);
  297. printf("Tests passed, you are Awsome!\n");
  298. printf("\nNow testing makeAction, for OBTAIN_PUBLICATION for player %d\n", activePlayer);
  299. givePlayersStudents(p1, p2, p3, 5);
  300. g = getGameWithStudents(p1, p2, p3, activePlayer, null_roll);
  301. testActionObtainPublication(g, activePlayer);
  302. disposeGame(g);
  303. printf("Tests passed, you are Awsome!\n");
  304. printf("\nNow testing makeAction, for OBTAIN_IP_PATENT for player %d\n", activePlayer);
  305. givePlayersStudents(p1, p2, p3, 5);
  306. g = getGameWithStudents(p1, p2, p3, activePlayer, null_roll);
  307. testActionObtainIPPatent(g, activePlayer);
  308. disposeGame(g);
  309. printf("Tests passed, you are Awsome!\n");
  310. printf("\nNow testing makeAction, for RETRAIN_STUDENTS for player %d\n", activePlayer);
  311. i=1;
  312. while (i<NUM_STUDENT_TYPES){
  313. j = 1;
  314. while (j<NUM_STUDENT_TYPES){;
  315. if(i != j){
  316. printf("between Student type %d and Student type %d\n", i,j);
  317. givePlayersStudents(p1, p2, p3, 5);
  318. g = getGameWithStudents(p1, p2, p3, activePlayer, null_roll);
  319. testActionRetrainStudents(g, activePlayer, i, j);
  320. disposeGame(g);
  321. }
  322. j++;
  323. }
  324. i++;
  325. }
  326. printf("Tests passed, you are Awsome!\n");
  327. activePlayer++;
  328. }
  329. }
  330. static void testActionRetrainStudents(Game g, int activePlayer, int disciplineFrom, int disciplineTo){
  331. action theAction;
  332. int exchangeRateBefore[NUM_STUDENT_TYPES][NUM_STUDENT_TYPES];
  333. int exchangeRateAfter[NUM_STUDENT_TYPES][NUM_STUDENT_TYPES];
  334. int studentsBefore[NUM_STUDENT_TYPES];
  335. int studentsAfter[NUM_STUDENT_TYPES];
  336. int i,j;
  337. int cost;
  338. theAction.disciplineFrom = disciplineFrom;
  339. theAction.disciplineTo = disciplineTo;
  340. theAction.actionCode = RETRAIN_STUDENTS;
  341. getPathToVertex(getVertexIndex(0), 0, theAction.destination);
  342. cost = getExchangeRate (g, activePlayer, disciplineFrom, disciplineTo);
  343. i=0;
  344. while (i < NUM_STUDENT_TYPES){
  345. j = 0; // can't retrain type 0 (THDs).. lol. (:
  346. while (j < NUM_STUDENT_TYPES){
  347. exchangeRateBefore[i][j] = getExchangeRate (g, activePlayer, i, j);
  348. j++;
  349. }
  350. i++;
  351. }
  352. i=0;
  353. while (i < NUM_STUDENT_TYPES){
  354. studentsBefore[i] = getStudents(g, activePlayer, i);
  355. i++;
  356. }
  357. makeAction(g, theAction);
  358. i=0;
  359. while (i < NUM_STUDENT_TYPES){
  360. j = 0;
  361. while (j < NUM_STUDENT_TYPES){
  362. exchangeRateAfter[i][j] = getExchangeRate (g, activePlayer, i, j);
  363. j++;
  364. }
  365. i++;
  366. }
  367. i=0;
  368. while (i < NUM_STUDENT_TYPES){
  369. studentsAfter[i] = getStudents(g, activePlayer, i);
  370. i++;
  371. }
  372. // retraining checks..
  373. // tests every valid permutation of
  374. // disciplineTo's and disciplineFrom's
  375. printf("checking that the exchange rates have not changed\n");
  376. i = 1;
  377. while (i < NUM_STUDENT_TYPES){
  378. j = 0;
  379. while (j < NUM_STUDENT_TYPES){
  380. assert(exchangeRateAfter[i][j] == exchangeRateBefore[i][j]);
  381. j++;
  382. }
  383. i++;
  384. }
  385. printf("checking that the number of students has changed in the\n");
  386. printf("right way\n");
  387. i=0;
  388. while (i < NUM_STUDENT_TYPES){
  389. if (i == disciplineFrom){
  390. assert(studentsAfter[i] == (studentsBefore[i] - cost));
  391. } else if (i == disciplineTo){
  392. assert(studentsAfter[i] == (studentsBefore[i] + 1));
  393. } else {
  394. assert(studentsBefore[i] == studentsAfter[i]);
  395. }
  396. i++;
  397. }
  398. //TODO: more tests.
  399. }
  400. static void testActionObtainIPPatent(Game g, int activePlayer){
  401. action theAction;
  402. theAction.actionCode = OBTAIN_IP_PATENT;
  403. getPathToVertex(getVertexIndex(0), 0, theAction.destination);
  404. int IPsBefore = getIPs(g, activePlayer);
  405. int KPIsBefore = getKPIpoints(g, activePlayer);
  406. int studentMJBefore = getStudents(g, activePlayer, STUDENT_MJ);
  407. int studentMMBefore = getStudents(g, activePlayer, STUDENT_MMONEY);
  408. int studentMTVBefore = getStudents(g, activePlayer, STUDENT_MTV);
  409. printf("doing makeAction with OBTAIN_IP_PATENT\n");
  410. makeAction(g, theAction);
  411. int IPsAfter = getIPs(g, activePlayer);
  412. int KPIsAfter = getKPIpoints(g, activePlayer);
  413. int studentMJAfter = getStudents(g, activePlayer, STUDENT_MJ);
  414. int studentMMAfter = getStudents(g, activePlayer, STUDENT_MMONEY);
  415. int studentMTVAfter = getStudents(g, activePlayer, STUDENT_MTV);
  416. printf("Checking that OBTAIN_PUBLICATION has the right cost\n");
  417. assert(studentMJAfter == studentMJBefore- 1);
  418. assert(studentMMAfter == studentMMBefore - 1);
  419. assert(studentMTVAfter == studentMTVBefore - 1);
  420. // give 10 more KPI points
  421. printf("checking that the number of KPIs has been incremented by 10\n");
  422. assert(KPIsAfter == KPIsBefore + 10);
  423. // increment getIPs()
  424. printf("checking that the number of IPs has been incremented by 1\n");
  425. assert(IPsAfter == IPsBefore + 1);
  426. // should not change anything else (just checking no. campuses atm)
  427. }
  428. static void testActionObtainPublication(Game g, int activePlayer){
  429. action theAction;
  430. theAction.actionCode = OBTAIN_PUBLICATION;
  431. getPathToVertex(getVertexIndex(0), 0, theAction.destination);
  432. int studentMJBefore = getStudents(g, activePlayer, STUDENT_MJ);
  433. int studentMMBefore = getStudents(g, activePlayer, STUDENT_MMONEY);
  434. int studentMTVBefore = getStudents(g, activePlayer, STUDENT_MTV);
  435. printf("Now running makeAction with OBTAIN_PUBLICATION\n");
  436. makeAction(g, theAction);
  437. int studentMJAfter = getStudents(g, activePlayer, STUDENT_MJ);
  438. int studentMMAfter = getStudents(g, activePlayer, STUDENT_MMONEY);
  439. int studentMTVAfter = getStudents(g, activePlayer, STUDENT_MTV);
  440. // cost of publication
  441. printf("Checking that OBTAIN_PUBLICATION has the right cost\n");
  442. assert(studentMJAfter == studentMJBefore- 1);
  443. assert(studentMMAfter == studentMMBefore - 1);
  444. assert(studentMTVAfter == studentMTVBefore - 1);
  445. // todo: max no of publication bonus KPI points check
  446. }
  447. static void testActionObtainARC(Game g, int activePlayer){
  448. action theAction;
  449. int edgeIndex[4] = {0, 5, 26, 54};
  450. getPathToEdge(edgeIndex[activePlayer], 0, theAction.destination);
  451. theAction.actionCode = OBTAIN_ARC;
  452. int edgeCode[NUM_EDGES];
  453. path tempPath;
  454. int i = 0;
  455. while (i < NUM_EDGES){
  456. getPathToEdge(i, 0, tempPath);
  457. edgeCode[i] = getARC(g, tempPath);
  458. i++;
  459. }
  460. int KPIsBefore = getKPIpoints(g, activePlayer);
  461. int ARCsBefore = getARCs(g, activePlayer);
  462. int studentBPSBefore = getStudents(g, activePlayer, STUDENT_BPS);
  463. int studentBQNBefore = getStudents(g, activePlayer, STUDENT_BQN);
  464. makeAction(g, theAction);
  465. int KPIsAfter = getKPIpoints(g, activePlayer);
  466. int ARCsAfter = getARCs(g, activePlayer);
  467. int studentBPSAfter = getStudents(g, activePlayer, STUDENT_BPS);
  468. int studentBQNAfter = getStudents(g, activePlayer, STUDENT_BQN);
  469. // cost of obtaining ARC
  470. assert(studentBPSAfter == studentBPSBefore - 1);
  471. assert(studentBQNAfter == studentBQNBefore - 1);
  472. // increment number of ARCs for player
  473. printf("Checking that the number of ARCs is incremented by 1\n");
  474. assert(ARCsAfter == ARCsBefore + 1);
  475. // increase value of KPI by 12
  476. printf("Checking that the number of GO8s is incremented by 12\n");
  477. assert(KPIsAfter == KPIsBefore + 12);
  478. printf("checking that the only edge that has changed is the\n");
  479. printf("arc that we changed\n");
  480. i = 0;
  481. while (i < NUM_EDGES){
  482. getPathToEdge(i, 0, tempPath);
  483. if(i == edgeIndex[activePlayer]){
  484. assert((activePlayer) == getARC(g, tempPath));
  485. } else {
  486. assert(edgeCode[i] == getARC(g, tempPath));
  487. }
  488. i++;
  489. }
  490. // TODO - stuff happening with leader of ARCs and such.
  491. }
  492. static void testActionBuildCampus(Game g, int activePlayer){
  493. action theAction;
  494. int arcIndex[4] = {0, 5, 26, 54};
  495. int vertexIndex[4] = {0,8,19,37};
  496. theAction.actionCode = OBTAIN_ARC;
  497. getPathToEdge(arcIndex[activePlayer], 0, theAction.destination);
  498. printf("doing OBTAIN_ARC on path %s\n", theAction.destination);
  499. makeAction(g, theAction);
  500. theAction.actionCode = BUILD_CAMPUS;
  501. getPathToVertex(vertexIndex[activePlayer], 0, theAction.destination);
  502. int campusCode[NUM_VERTICIES];
  503. path tempPath;
  504. int i = 0;
  505. while (i < NUM_VERTICIES){
  506. getPathToVertex(i, 0, tempPath);
  507. campusCode[i] = getCampus(g, tempPath);
  508. i++;
  509. }
  510. int campusesBefore = getCampuses(g, activePlayer);
  511. int KPIPointsBefore = getKPIpoints(g, activePlayer);
  512. int studentBPSBefore = getStudents(g, activePlayer, STUDENT_BPS);
  513. int studentBQNBefore = getStudents(g, activePlayer, STUDENT_BQN);
  514. int studentMJBefore = getStudents(g, activePlayer, STUDENT_MJ);
  515. int studentMTVBefore = getStudents(g, activePlayer, STUDENT_MTV);
  516. printf("doing action BUILD_CAMPUS on path %s\n", theAction.destination);
  517. makeAction(g, theAction);
  518. int campusesAfter = getCampuses(g, activePlayer);
  519. int KPIPointsAfter = getKPIpoints(g, activePlayer);
  520. int studentBPSAfter = getStudents(g, activePlayer, STUDENT_BPS);
  521. int studentBQNAfter = getStudents(g, activePlayer, STUDENT_BQN);
  522. int studentMJAfter = getStudents(g, activePlayer, STUDENT_MJ);
  523. int studentMTVAfter = getStudents(g, activePlayer, STUDENT_MTV);
  524. printf("Checking that the number of Campuses is incremented by 1\n");
  525. //Number of campuses incremenet
  526. assert(campusesAfter == campusesBefore + 1);
  527. printf("Checking that the number of KPIs is incremented by 10\n");
  528. // KPI points increment
  529. assert(KPIPointsAfter == KPIPointsBefore + 10);
  530. printf("Checking that the number of students has been decremented\n");
  531. printf("by the right amount\n");
  532. //cost of building campus
  533. assert(studentBPSAfter == studentBPSBefore - 1);
  534. assert(studentBQNAfter == studentBQNBefore - 1);
  535. assert(studentMJAfter == studentMJBefore - 1);
  536. assert(studentMTVAfter == studentMTVBefore - 1);
  537. //change in tile state - checks every single tile!
  538. i = 0;
  539. while (i < NUM_VERTICIES){
  540. getPathToVertex(i, 0, tempPath);
  541. if(i == vertexIndex[activePlayer]){
  542. assert((activePlayer) == getCampus(g, tempPath));
  543. } else {
  544. assert(campusCode[i] == getCampus(g, tempPath));
  545. }
  546. i++;
  547. }
  548. }
  549. static void testActionBuildGO8(Game g, int activePlayer){
  550. action theAction;
  551. theAction.actionCode = BUILD_GO8;
  552. int index[NUM_UNIS+1] = {0, 0, 1, 2};
  553. getPathToVertex(getVertexIndex(index[activePlayer]), 0, theAction.destination);
  554. int campusCode[NUM_VERTICIES];
  555. path tempPath;
  556. int i = 0;
  557. while (i < NUM_VERTICIES){
  558. getPathToVertex(i, 0, tempPath);
  559. campusCode[i] = getCampus(g, tempPath);
  560. i++;
  561. }
  562. int campusesBefore = getCampuses(g, activePlayer);
  563. int GO8sBefore = getGO8s(g, activePlayer);
  564. int KPIsBefore = getKPIpoints(g, activePlayer);
  565. int studentMJBefore= getStudents(g, activePlayer, STUDENT_MJ);
  566. int studentMMBefore= getStudents(g, activePlayer, STUDENT_MMONEY);
  567. printf("doing BUILD_GO8 on path %s\n", theAction.destination);
  568. makeAction(g, theAction);
  569. int campusesAfter = getCampuses(g, activePlayer);
  570. int GO8sAfter = getGO8s(g, activePlayer);
  571. int KPIsAfter = getKPIpoints(g, activePlayer);
  572. int studentMJAfter = getStudents(g, activePlayer, STUDENT_MJ);
  573. int studentMMAfter = getStudents(g, activePlayer, STUDENT_MMONEY);
  574. // decrement in number of campuses
  575. printf("Checking that the number of campuses is decremented by 1\n");
  576. assert(campusesAfter == campusesBefore - 1);
  577. // increment in number of GO8s
  578. printf("Checking that the number of GO8s is incremented by 1\n");
  579. assert(GO8sAfter == GO8sBefore + 1);
  580. // KPI + 10
  581. printf("Checking that a player has 10 more KPIs than before\n");
  582. assert(KPIsAfter == KPIsBefore + 10);
  583. // cost of building
  584. printf("Checking that the number of students you have is\n");
  585. printf("decreased by the right ammount\n");
  586. assert(studentMJAfter == studentMJBefore - 2);
  587. assert(studentMMAfter == studentMMBefore - 3);
  588. //change in tile state - checking in EVERY tile.
  589. printf("checking that the board has only changed by converting a\n");
  590. printf("camapus to a GO8 of the right type and in the right spot\n");
  591. i = 0;
  592. while (i < NUM_VERTICIES){
  593. getPathToVertex(i, 0, tempPath);
  594. if(i == getVertexIndex(index[activePlayer])){
  595. assert((activePlayer + 3) == getCampus(g, tempPath));
  596. } else {
  597. assert(campusCode[i] == getCampus(g, tempPath));
  598. }
  599. i++;
  600. }
  601. }
  602. static void testGetKPIPoints(void){
  603. Game g;
  604. int p1[NUM_STUDENT_TYPES];
  605. int p2[NUM_STUDENT_TYPES];
  606. int p3[NUM_STUDENT_TYPES];
  607. int activePlayer = 1;
  608. int null_roll = 4;
  609. int noOfCampuses;
  610. int noOfGO8;
  611. int noOfARC;
  612. int noOfIP;
  613. int hasLargestARC;
  614. int hasLargestPubs;
  615. int expectedKPI;
  616. givePlayersStudents(p1, p2, p3, 1);
  617. while (activePlayer < NUM_UNIS + 1){
  618. g = getGameWithStudents(p1, p2, p3, activePlayer, null_roll);
  619. noOfCampuses = getCampuses(g,activePlayer);
  620. noOfGO8 = getGO8s(g,activePlayer);
  621. noOfARC = getARCs(g,activePlayer);
  622. noOfIP = getIPs(g,activePlayer);
  623. // this is a cheap hack.
  624. hasLargestARC = (getMostARCs (g) == activePlayer);
  625. hasLargestPubs = (getMostPublications (g) == activePlayer);
  626. expectedKPI = 10*noOfCampuses + 20*noOfGO8 + 2*noOfARC
  627. + 10*noOfIP + 10*hasLargestARC + 10*hasLargestPubs;
  628. // testing correct calculation of KPI
  629. assert(getKPIpoints(g, activePlayer) == expectedKPI);
  630. /* TODO (WT#1#): test for change of game state */
  631. activePlayer++;
  632. disposeGame (g);
  633. }
  634. }
  635. //Alexis Shaw's test code
  636. static Game getGameWithStudentsSmall(int studentType, int Player, int amount,
  637. int nullRoll){
  638. int regions[19] = {studentType,0,studentType,0,0,0,0,studentType,
  639. 0,0,0,0,0,0,0,0,0,0,0};
  640. int dice[2][19] = {{1,0,2,0,0,0,0,3,0,0,0,4,0,0,0,0,5,0,6},
  641. {8,0,9,0,0,0,0,10,0,0,0,11,0,0,0,0,12,0,13}};
  642. int diceToRoll[2][3][2] = {{{3,4},{1,6},{2,5}},
  643. {{10,11},{8,13},{9,13}}};
  644. int diceSet;
  645. if(nullRoll<7){
  646. diceSet = 0;
  647. } else {
  648. diceSet = 1;
  649. }
  650. Game g = newGame (regions, dice[diceSet]);
  651. int k;
  652. k = 0;
  653. while(k<amount){
  654. throwDice(g, diceToRoll[diceSet][Player][0]);
  655. k++;
  656. }
  657. while(getWhoseTurn (g) != Player){
  658. throwDice(g, nullRoll);
  659. }
  660. return g;
  661. }
  662. static Game getGameWithStudents(int p1[], int p2[], int p3[],int activePlayer,int nullRoll){
  663. int regions[19] = {STUDENT_BPS,0,STUDENT_BPS,0,0,0,0,STUDENT_BPS,
  664. 0,0,0,STUDENT_THD,0,0,0,0,STUDENT_THD,0,STUDENT_THD};
  665. int dice[2][19] = {{1,0,2,0,0,0,0,3,0,0,0,4,0,0,0,0,5,0,6},
  666. {8,0,9,0,0,0,0,10,0,0,0,11,0,0,0,0,12,0,13}};
  667. int diceToRoll[2][3][2] = {{{3,4},{1,6},{2,5}},
  668. {{10,11},{8,13},{9,13}}};
  669. int diceSet;
  670. int students[3][6];
  671. int i,j,k;
  672. i=0;
  673. while (i<6) {
  674. students[0][i] = p1[i];
  675. i++;
  676. }
  677. i=0;
  678. while (i<6) {
  679. students[1][i] = p2[i];
  680. i++;
  681. }
  682. i=0;
  683. while (i<6) {
  684. students[2][i] = p3[i];
  685. i++;
  686. }
  687. if(nullRoll<7){
  688. diceSet = 0;
  689. } else {
  690. diceSet = 1;
  691. }
  692. Game g = newGame (regions, dice[diceSet]);
  693. throwDice(g, nullRoll);
  694. i=0;
  695. action myAction;
  696. myAction.disciplineFrom = STUDENT_BPS;
  697. myAction.actionCode = RETRAIN_STUDENTS;
  698. while (i < 3){
  699. j = 0;
  700. while (j<6){
  701. k = 0;
  702. while(k<students[i][j]){
  703. if(j == STUDENT_THD){
  704. throwDice(g, diceToRoll[diceSet][i][1]);
  705. throwDice(g, nullRoll);
  706. throwDice(g, nullRoll);
  707. } else if(j == STUDENT_BPS){
  708. throwDice(g, diceToRoll[diceSet][i][0]);
  709. throwDice(g, nullRoll);
  710. throwDice(g, nullRoll);
  711. } else {
  712. myAction.disciplineTo = j;
  713. throwDice(g, diceToRoll[diceSet][i][0]);
  714. throwDice(g, nullRoll);
  715. throwDice(g, nullRoll);
  716. throwDice(g, diceToRoll[diceSet][i][0]);
  717. throwDice(g, nullRoll);
  718. throwDice(g, nullRoll);
  719. throwDice(g, diceToRoll[diceSet][i][0]);
  720. throwDice(g, nullRoll);
  721. throwDice(g, nullRoll);
  722. makeAction(g, myAction);
  723. }
  724. k++;
  725. }
  726. j++;
  727. }
  728. throwDice(g, nullRoll);
  729. i++;
  730. }
  731. while((getWhoseTurn (g) != activePlayer)){
  732. throwDice(g, nullRoll);
  733. }
  734. return g;
  735. }
  736. static int getEdgeIndex(int specialPoint){
  737. int specialPointArray[6] = {1, 18, 60, 70, 51, 15};
  738. return specialPointArray[specialPoint];
  739. }
  740. static void getPathToEdge(int index, int type, path destination){
  741. edge edges[150];
  742. getArrayOfEdges(edges);
  743. point start = {2, 10};
  744. point back = {-100, -100};
  745. getPathThroughEdge(start,back,edges[index],destination);
  746. }
  747. static int getArrayOfEdges(edge edges[]){
  748. int i = 0;
  749. int row = 10;
  750. while(row>=0){
  751. int col=0;
  752. while( col<=10){
  753. if(isVertex(row, col) && isOnBoard(row, col)){
  754. if(isVertex(row, col+1) && isOnBoard(row, col+1)){
  755. edges[i].p1.x = col;
  756. edges[i].p1.y = row;
  757. edges[i].p2.x = col+1;
  758. edges[i].p2.y = row;
  759. i++;
  760. }
  761. if(isVertex(row-1, col) && isOnBoard(row-1,col)){
  762. edges[i].p1.x = col;
  763. edges[i].p1.y = row;
  764. edges[i].p2.x = col;
  765. edges[i].p2.y = row-1;
  766. i++;
  767. }
  768. if(isVertex(row-1,col+1)&& isOnBoard(row-1, col+1)){
  769. edges[i].p1.x = col;
  770. edges[i].p1.y = row;
  771. edges[i].p2.x = col+1;
  772. edges[i].p2.y = row-1;
  773. i++;
  774. }
  775. }
  776. col++;
  777. }
  778. row --;
  779. }
  780. return i;
  781. }
  782. static int getVertexIndex(int specialPoint){
  783. int specialPointArray[6] = {0, 6, 42, 53, 41, 11};
  784. return specialPointArray[specialPoint];
  785. }
  786. static void getPathToVertex(int index, int type, path destination){
  787. point points[100];
  788. getArrayOfVertecies(points);
  789. point start = {2, 10};
  790. point back = {-100, -100};
  791. getPath(start,back,points[index],destination);
  792. }
  793. static int isOnBoard(int row,int col){
  794. return (((row+2*col)>= 7)&& ((2*row+col)>= 7) && ((row-col) <= 8) &&
  795. ((2*row + col) <= 23) && ((row+2*col) <= 23) && ((col- row) <= 8));
  796. }
  797. static int isVertex(int row,int col){
  798. return ((row - col)%3 != 0);
  799. }
  800. static int getArrayOfVertecies(point points[]){
  801. int i = 0;
  802. int row = 10;
  803. while(row>=0){
  804. int col=0;
  805. while( col<=10){
  806. if(isVertex(row, col) && isOnBoard(row, col)){
  807. points[i].x = col;
  808. points[i].y = row;
  809. i++;
  810. }
  811. col++;
  812. }
  813. row --;
  814. }
  815. return i;
  816. }
  817. static double getDistance(point p1, point point2){
  818. double ix1;
  819. double iy1;
  820. double iy2;
  821. double ix2;
  822. double xBasisx = 1;
  823. double xBasisy = 0;
  824. double yBasisx = cos(M_PI/3);
  825. double yBasisy = sin(M_PI/3);
  826. ix1 = p1.x*xBasisx + p1.y*yBasisx;
  827. iy1 = p1.x*xBasisy + p1.y*yBasisy;
  828. ix2 = point2.x*xBasisx + point2.y*yBasisx;
  829. iy2 = point2.x*xBasisy + point2.y*yBasisy;
  830. return (ix1-ix2)*(ix1-ix2) + (iy1-iy2)*(iy1-iy2);
  831. }
  832. static void getPathThroughEdge(point start,point back,edge end,path destination){
  833. point currentNode = start;
  834. point currentBackDirection = back;
  835. double Ldist;
  836. double Rdist;
  837. double Bdist;
  838. char next[3] = {0};
  839. destination[0] = '\0';
  840. point temp;
  841. while((currentNode.x != end.p1.x) || (currentNode.y != end.p1.y)){
  842. Bdist = getDistance(end.p1, currentBackDirection);
  843. Rdist = getDistance(end.p1, getRight(currentNode, currentBackDirection));
  844. Ldist = getDistance(end.p1, getLeft(currentNode, currentBackDirection));
  845. if(Rdist < Ldist){
  846. if (Rdist < Bdist){
  847. next[0] = 'R';
  848. } else {
  849. next[0] = 'B';
  850. }
  851. } else if (Rdist > Ldist){
  852. if (Ldist < Bdist){
  853. next[0] = 'L';
  854. } else {
  855. next[0] = 'B';
  856. }
  857. } else {
  858. if (Bdist < Rdist){
  859. next[0] = 'B';
  860. } else {
  861. next[0] = 'R';
  862. }
  863. }
  864. strcat (destination, next);
  865. temp = currentNode;
  866. if(next[0] == 'L'){
  867. currentNode = getLeft(currentNode, currentBackDirection);
  868. currentBackDirection = temp;
  869. } else if (next [0]== 'R'){
  870. currentNode = getRight(currentNode, currentBackDirection);
  871. currentBackDirection = temp;
  872. } else if (next[0] == 'B'){
  873. currentNode = currentBackDirection;
  874. currentBackDirection = temp;
  875. }
  876. }
  877. if(end.p2.x == currentBackDirection.x && end.p2.y == currentBackDirection.y){
  878. next[0] = 'B';
  879. } else if (end.p2.x == getLeft(currentNode, currentBackDirection).x
  880. && end.p2.y == getLeft(currentNode, currentBackDirection).y){
  881. next[0] = 'L';
  882. } else if (end.p2.x == getRight(currentNode, currentBackDirection).x
  883. && end.p2.y == getRight(currentNode, currentBackDirection).y){
  884. next[0] = 'R';
  885. }
  886. strcat (destination, next);
  887. }
  888. static void getPath(point start,point back,point end,path destination){
  889. point currentNode = start;
  890. point currentBackDirection = back;
  891. double Ldist;
  892. double Rdist;
  893. double Bdist;
  894. char next[3] = {0};
  895. destination[0] = '\0';
  896. point temp;
  897. while((currentNode.x != end.x) || (currentNode.y != end.y)){
  898. Bdist = getDistance(end, currentBackDirection);
  899. Rdist = getDistance(end, getRight(currentNode, currentBackDirection));
  900. Ldist = getDistance(end, getLeft(currentNode, currentBackDirection));
  901. if(Rdist < Ldist){
  902. if (Rdist < Bdist){
  903. next[0] = 'R';
  904. } else {
  905. next[0] = 'B';
  906. }
  907. } else if (Rdist > Ldist){
  908. if (Ldist < Bdist){
  909. next[0] = 'L';
  910. } else {
  911. next[0] = 'B';
  912. }
  913. } else {
  914. if (Bdist < Rdist){
  915. next[0] = 'B';
  916. } else {
  917. next[0] = 'R';
  918. }
  919. }
  920. strcat (destination, next);
  921. temp = currentNode;
  922. if(next[0] == 'L'){
  923. currentNode = getLeft(currentNode, currentBackDirection);
  924. currentBackDirection = temp;
  925. } else if (next [0]== 'R'){
  926. currentNode = getRight(currentNode, currentBackDirection);
  927. currentBackDirection = temp;
  928. } else if (next[0] == 'B'){
  929. currentNode = currentBackDirection;
  930. currentBackDirection = temp;
  931. }
  932. }
  933. }
  934. static point getRight(point input,point back){
  935. int backcount;
  936. int i=0;
  937. point surroundingVertecies[3];
  938. getSurroundingVerticies(input, surroundingVertecies);
  939. while(i<3){
  940. if((surroundingVertecies[i].x == back.x) && (surroundingVertecies[i].y == back.y)){
  941. backcount = i;
  942. }
  943. i++;
  944. }
  945. return surroundingVertecies[(backcount+2)%3];
  946. }
  947. static point getLeft(point input,point back){
  948. int backcount;
  949. int i=0;
  950. point surroundingVertecies[3];
  951. getSurroundingVerticies(input, surroundingVertecies);
  952. while(i<3){
  953. if((surroundingVertecies[i].x == back.x) && (surroundingVertecies[i].y == back.y)){
  954. backcount = i;
  955. }
  956. i++;
  957. }
  958. return surroundingVertecies[(backcount+1)%3];
  959. }
  960. static void getSurroundingVerticies(point p1, point surroundingVertecies[]){
  961. int i = 0;
  962. if(isVertex(p1.x,p1.y+1)){
  963. point temp = {p1.x, p1.y +1};
  964. surroundingVertecies[i] = temp;
  965. i++;
  966. }
  967. if(isVertex(p1.x+1,p1.y)){
  968. point temp = {p1.x+1, p1.y};
  969. surroundingVertecies[i] = temp;
  970. i++;
  971. }
  972. if(isVertex(p1.x+1,p1.y-1)){
  973. point temp = {p1.x+1, p1.y -1};
  974. surroundingVertecies[i] = temp;
  975. i++;
  976. }
  977. if(isVertex(p1.x,p1.y-1)){
  978. point temp = {p1.x, p1.y-1};
  979. surroundingVertecies[i] = temp;
  980. i++;
  981. }
  982. if(isVertex(p1.x-1,p1.y)){
  983. point temp = {p1.x-1, p1.y};
  984. surroundingVertecies[i] = temp;
  985. i++;
  986. }
  987. if(isVertex(p1.x-1,p1.y+1)){
  988. point temp = {p1.x-1, p1.y+1};
  989. surroundingVertecies[i] = temp;
  990. i++;
  991. }
  992. for(i=0; i<3; i++){
  993. if(!isOnBoard(surroundingVertecies[i].x, surroundingVertecies[i].y)){
  994. surroundingVertecies[i].x = -100;
  995. surroundingVertecies[i].y = -100;
  996. }
  997. }
  998. }