Advertisement
Guest User

rhgefawd

a guest
Oct 23rd, 2019
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 24.19 KB | None | 0 0
  1. // Skeleton Program for the AQA A1 Summer 2019 examination
  2. // this code should be used in conjunction with the Preliminary Material
  3. // written by the AQA AS1 Programmer Team
  4. // developed in Visual Studio 2017
  5.  
  6. // Version number: 0.0.3
  7.  
  8. using System;
  9. using System.Collections.Generic;
  10. using System.Linq;
  11. using System.Text;
  12. using System.Threading.Tasks;
  13. using System.IO;
  14.  
  15. namespace BoardGameCS
  16. {
  17. class Program
  18. {
  19. const string Space = " ";
  20. const string Unused = "XXXXX";
  21. const int BoardSize = 8;
  22. const int NumberOfPieces = 12;
  23. const int MaxMoves = 50;
  24. const int Row = 0;
  25. const int Column = 1;
  26. const int Dame = 2;
  27.  
  28. struct MoveRecord
  29. {
  30. public string Piece;
  31. public int NewRow;
  32. public int NewColumn;
  33. public bool CanJump;
  34. }
  35.  
  36. private static void LoadPieces(StreamReader fileHandle, int[,] playersPieces)
  37. {
  38. for (int index = 0; index < NumberOfPieces + 1; index++)
  39. {
  40. playersPieces[index, Row] = Convert.ToInt32(fileHandle.ReadLine());
  41. playersPieces[index, Column] = Convert.ToInt32(fileHandle.ReadLine());
  42. playersPieces[index, Dame] = Convert.ToInt32(fileHandle.ReadLine());
  43. }
  44. }
  45.  
  46. private static void CreateNewBoard(string[,] board)
  47. {
  48. for (int thisRow = 0; thisRow < BoardSize; thisRow++)
  49. {
  50. for (int thisColumn = 0; thisColumn < BoardSize; thisColumn++)
  51. {
  52. if ((thisRow + thisColumn) % 2 == 0)
  53. {
  54. board[thisRow, thisColumn] = Unused;
  55. }
  56. else
  57. {
  58. board[thisRow, thisColumn] = Space;
  59. }
  60. }
  61. }
  62. }
  63.  
  64. private static void AddPlayerA(string[,] board, int[,] a)
  65. {
  66. int pieceRow, pieceColumn, pieceDame;
  67. for (int index = 1; index < NumberOfPieces + 1; index++)
  68. {
  69. pieceRow = a[index, Row];
  70. pieceColumn = a[index, Column];
  71. pieceDame = a[index, Dame];
  72. if (pieceRow > -1)
  73. {
  74. if (pieceDame == 1)
  75. {
  76. board[pieceRow, pieceColumn] = "A" + index;
  77. }
  78. else
  79. {
  80. board[pieceRow, pieceColumn] = "a" + index;
  81. }
  82. }
  83. }
  84. }
  85.  
  86. private static void AddPlayerB(string[,] board, int[,] b)
  87. {
  88. int pieceRow, pieceColumn, pieceDame;
  89. for (int index = 1; index < NumberOfPieces + 1; index++)
  90. {
  91. pieceRow = b[index, Row];
  92. pieceColumn = b[index, Column];
  93. pieceDame = b[index, Dame];
  94. if (pieceRow > -1)
  95. {
  96. if (pieceDame == 1)
  97. {
  98. board[pieceRow, pieceColumn] = "B" + index;
  99. }
  100. else
  101. {
  102. board[pieceRow, pieceColumn] = "b" + index;
  103. }
  104. }
  105. }
  106. }
  107.  
  108. private static void DisplayErrorCode(int errorNumber)
  109. {
  110. Console.WriteLine("Error " + errorNumber);
  111. }
  112.  
  113. private static void SetUpBoard(string[,] board, int[,] a, int[,] b, ref bool fileFound)
  114. {
  115. string fileName = "game1.txt";
  116. string answer = "";
  117. Boolean valid = false;
  118. while (!valid)
  119. {
  120. Console.Write("Do you want to load a saved game? (Y/N): ");
  121. answer = Console.ReadLine();
  122. if (answer == "Y" || answer == "y")
  123. {
  124. Boolean validFile = false;
  125. while (!validFile)
  126. {
  127. Console.Write("Enter the filename: ");
  128. fileName = Console.ReadLine();
  129. if (fileName == "game1.txt" || fileName == "game2.txt" || fileName == "game3.txt" || fileName == "game4.txt" || fileName == "AutoSave.txt")
  130. {
  131. validFile = true;
  132. }
  133. }
  134. valid = true;
  135.  
  136. }
  137. try
  138. {
  139. StreamReader filehandle = new StreamReader(fileName);
  140. fileFound = true;
  141. LoadPieces(filehandle, a);
  142. LoadPieces(filehandle, b);
  143. filehandle.Close();
  144. CreateNewBoard(board);
  145. AddPlayerA(board, a);
  146. AddPlayerB(board, b);
  147. }
  148. catch (Exception)
  149. {
  150. DisplayErrorCode(4);
  151. }
  152. if (answer == "N" || answer == "n")
  153. {
  154. Environment.Exit(0);
  155. }
  156. }
  157.  
  158.  
  159. }
  160.  
  161. private static void PrintHeading()
  162. {
  163. Console.Write(" ");
  164. for (int boardColumn = 0; boardColumn < BoardSize; boardColumn++)
  165. {
  166. Console.Write("{0,3}{1}", boardColumn, " ");
  167. }
  168. Console.WriteLine();
  169. }
  170.  
  171. private static void PrintRow(string[,] board, int thisRow)
  172. {
  173. Console.Write(" |");
  174. for (int boardColumn = 0; boardColumn < BoardSize; boardColumn++)
  175. {
  176. if (board[thisRow, boardColumn] == Unused)
  177. {
  178. Console.Write(board[thisRow, boardColumn] + "|");
  179. }
  180. else
  181. {
  182. Console.Write(Space + "|"); ;
  183. }
  184. }
  185. Console.WriteLine();
  186. }
  187.  
  188. private static void PrintMiddleRow(string[,] board, int thisRow)
  189. {
  190. Console.Write("{0,2}{1}", thisRow, " |");
  191. for (int boardColumn = 0; boardColumn < BoardSize; boardColumn++)
  192. {
  193. if (board[thisRow, boardColumn] == Unused || board[thisRow, boardColumn] == Space)
  194. {
  195. Console.Write(board[thisRow, boardColumn] + "|");
  196. }
  197. else
  198. {
  199. Console.Write("{0,4}{1}", board[thisRow, boardColumn], " |");
  200. }
  201. }
  202. Console.WriteLine();
  203. }
  204.  
  205. private static void PrintLine()
  206. {
  207. Console.Write(" ");
  208. for (int boardColumn = 0; boardColumn < BoardSize; boardColumn++)
  209. {
  210. Console.Write("------");
  211. }
  212. Console.WriteLine("-");
  213. }
  214.  
  215. private static void DisplayBoard(string[,] board)
  216. {
  217. PrintHeading();
  218. PrintLine();
  219. for (int thisRow = 0; thisRow < BoardSize; thisRow++)
  220. {
  221. PrintRow(board, thisRow);
  222. PrintMiddleRow(board, thisRow);
  223. PrintRow(board, thisRow);
  224. PrintLine();
  225. }
  226. }
  227.  
  228. private static void PrintPlayerPieces(int[,] a, int[,] b)
  229. {
  230. List<int> savelist = new List<int>();
  231. Console.WriteLine();
  232. Console.WriteLine("Player A:");
  233. Console.Write("[");
  234. for (int index = 0; index < NumberOfPieces; index++)
  235. {
  236. Console.Write("[" + a[index, Row] + ", " + a[index, Column] + ", " + a[index, Dame] + "], ");
  237. savelist.Add(a[index, Row]);
  238. savelist.Add(a[index, Column]);
  239. savelist.Add(a[index, Dame]);
  240.  
  241. }
  242. Console.WriteLine("[" + a[NumberOfPieces, Row] + ", " + a[NumberOfPieces, Column] + ", " + a[NumberOfPieces, Dame] + "]]");
  243. savelist.Add(a[NumberOfPieces, Row]);
  244. savelist.Add(a[NumberOfPieces, Column]);
  245. savelist.Add(a[NumberOfPieces, Dame]);
  246. Console.WriteLine("Player B:");
  247. Console.Write("[");
  248. for (int index = 0; index < NumberOfPieces; index++)
  249. {
  250. Console.Write("[" + b[index, Row] + ", " + b[index, Column] + ", " + b[index, Dame] + "], ");
  251. savelist.Add(b[index, Row]);
  252. savelist.Add(b[index, Column]);
  253. savelist.Add(b[index, Dame]);
  254. }
  255. Console.WriteLine("[" + b[NumberOfPieces, Row] + ", " + b[NumberOfPieces, Column] + ", " + b[NumberOfPieces, Dame] + "]]");
  256. savelist.Add(b[NumberOfPieces, Row]);
  257. savelist.Add(b[NumberOfPieces, Column]);
  258. savelist.Add(b[NumberOfPieces, Dame]);
  259. Save(savelist);
  260. Console.WriteLine();
  261. }
  262.  
  263. private static void ClearList(MoveRecord[] listOfMoves)
  264. {
  265. for (int index = 0; index < MaxMoves; index++)
  266. {
  267. listOfMoves[index].Piece = "";
  268. listOfMoves[index].NewRow = -1;
  269. listOfMoves[index].NewColumn = -1;
  270. listOfMoves[index].CanJump = false;
  271. }
  272. }
  273.  
  274. private static bool ValidMove(string[,] board, int newRow, int newColumn)
  275. {
  276. bool valid = false;
  277. if (newRow >= 0 && newRow < BoardSize &&
  278. newColumn >= 0 && newColumn < BoardSize)
  279. {
  280. if (board[newRow, newColumn] == Space)
  281. {
  282. valid = true;
  283. }
  284. }
  285. return valid;
  286. }
  287.  
  288. private static bool ValidJump(string[,] board, int[,] playersPieces, string piece, int newRow, int newColumn)
  289. {
  290. bool valid = false;
  291. string middlePiece = "";
  292. string player, oppositePiecePlayer, middlePiecePlayer;
  293. int index, currentRow, currentColumn, middlePieceRow, middlePieceColumn;
  294. player = piece[0].ToString().ToLower();
  295. index = Convert.ToInt32(piece.Substring(1));
  296. if (player == "a")
  297. {
  298. oppositePiecePlayer = "b";
  299. }
  300. else
  301. {
  302. oppositePiecePlayer = "a";
  303. }
  304. if (newRow >= 0 && newRow < BoardSize &&
  305. newColumn >= 0 && newColumn < BoardSize)
  306. {
  307. if (board[newRow, newColumn] == Space)
  308. {
  309. currentRow = playersPieces[index, Row];
  310. currentColumn = playersPieces[index, Column];
  311. middlePieceRow = (currentRow + newRow) / 2;
  312. middlePieceColumn = (currentColumn + newColumn) / 2;
  313. middlePiece = board[middlePieceRow, middlePieceColumn];
  314. middlePiecePlayer = middlePiece[0].ToString().ToLower();
  315. if (middlePiecePlayer != oppositePiecePlayer && middlePiecePlayer != " ")
  316. {
  317. valid = true;
  318. }
  319. }
  320. }
  321. return valid;
  322. }
  323.  
  324. private static void ListPossibleMoves(string[,] board, int[,] playersPieces, string nextPlayer, MoveRecord[] listOfMoves)
  325. {
  326. int direction, numberOfMoves = 0; ;
  327. int currentColumn, leftColumn, rightColumn;
  328. int jumpLeftColumn, jumpRightColumn;
  329. int currentRow, newRow, jumpRow;
  330. string piece;
  331.  
  332. if (nextPlayer == "a")
  333. {
  334. direction = 1;
  335. }
  336. else
  337. {
  338. direction = -1;
  339. }
  340. for (int i = 1; i < NumberOfPieces + 1; i++)
  341. {
  342. piece = nextPlayer + i;
  343. currentRow = playersPieces[i, Row];
  344. currentColumn = playersPieces[i, Column];
  345. if (playersPieces[i, Dame] == 1)
  346. {
  347. piece = piece.ToUpper();
  348. }
  349. newRow = currentRow + direction;
  350. leftColumn = currentColumn - 1;
  351. rightColumn = currentColumn + 1;
  352. if (ValidMove(board, newRow, leftColumn))
  353. {
  354. Console.WriteLine(piece + " can move to " + newRow + " , " + leftColumn);
  355. numberOfMoves++;
  356. listOfMoves[numberOfMoves].Piece = piece;
  357. listOfMoves[numberOfMoves].NewRow = newRow;
  358. listOfMoves[numberOfMoves].NewColumn = leftColumn;
  359. listOfMoves[numberOfMoves].CanJump = false;
  360. }
  361. if (ValidMove(board, newRow, rightColumn))
  362. {
  363. Console.WriteLine(piece + " can move to " + newRow + " , " + rightColumn);
  364. numberOfMoves++;
  365. listOfMoves[numberOfMoves].Piece = piece;
  366. listOfMoves[numberOfMoves].NewRow = newRow;
  367. listOfMoves[numberOfMoves].NewColumn = rightColumn;
  368. listOfMoves[numberOfMoves].CanJump = false;
  369. }
  370. jumpRow = currentRow + direction + direction;
  371. jumpLeftColumn = currentColumn - 2;
  372. jumpRightColumn = currentColumn + 2;
  373. if (ValidJump(board, playersPieces, piece, jumpRow, jumpLeftColumn))
  374. {
  375. Console.WriteLine(piece + " can jump to " + jumpRow + " , " + jumpLeftColumn);
  376. numberOfMoves++;
  377. listOfMoves[numberOfMoves].Piece = piece;
  378. listOfMoves[numberOfMoves].NewRow = jumpRow;
  379. listOfMoves[numberOfMoves].NewColumn = jumpLeftColumn;
  380. listOfMoves[numberOfMoves].CanJump = true;
  381. }
  382. if (ValidJump(board, playersPieces, piece, jumpRow, jumpRightColumn))
  383. {
  384. Console.WriteLine(piece + " can jump to " + jumpRow + " , " + jumpRightColumn);
  385. numberOfMoves++;
  386. listOfMoves[numberOfMoves].Piece = piece;
  387. listOfMoves[numberOfMoves].NewRow = jumpRow;
  388. listOfMoves[numberOfMoves].NewColumn = jumpRightColumn;
  389. listOfMoves[numberOfMoves].CanJump = true;
  390. }
  391. }
  392. Console.WriteLine("There are " + numberOfMoves + " possible moves");
  393. }
  394.  
  395. private static bool ListEmpty(MoveRecord[] listOfMoves)
  396. {
  397. if (listOfMoves[1].Piece == "")
  398. {
  399. return true;
  400. }
  401. else
  402. {
  403. return false;
  404. }
  405. }
  406.  
  407. private static int SelectMove(MoveRecord[] listOfMoves)
  408. {
  409. bool validPiece = false, validMove, found, endOfList;
  410. string piece = "", rowString, columnString;
  411. int index = 0, chosenPieceIndex;
  412. int newRow, newColumn;
  413. while (!validPiece)
  414. {
  415. found = false;
  416. endOfList = false;
  417. Console.Write("Which piece do you want to move? ");
  418. piece = Console.ReadLine();
  419. index = 0;
  420. if (piece == "")
  421. {
  422. endOfList = true;
  423. }
  424. while (!found && !endOfList)
  425. {
  426. index++;
  427. if (listOfMoves[index].Piece == piece)
  428. {
  429. found = true;
  430. }
  431. else if (listOfMoves[index].Piece == "")
  432. {
  433. endOfList = true;
  434. DisplayErrorCode(1);
  435. }
  436. }
  437. if (found)
  438. {
  439. validPiece = true;
  440. }
  441. }
  442. chosenPieceIndex = index;
  443. validMove = false;
  444. while (!validMove)
  445. {
  446. Console.Write("Which row do you want to move to? ");
  447. rowString = Console.ReadLine();
  448. Console.Write("Which column do you want to move to? ");
  449. columnString = Console.ReadLine();
  450. try
  451. {
  452. newRow = Convert.ToInt32(rowString);
  453. newColumn = Convert.ToInt32(columnString);
  454. found = false;
  455. endOfList = false;
  456. index = chosenPieceIndex - 1;
  457. while (!found && !endOfList)
  458. {
  459. index++;
  460. if (listOfMoves[index].Piece != piece)
  461. {
  462. endOfList = true;
  463. DisplayErrorCode(2);
  464. }
  465. else if (listOfMoves[index].NewRow == newRow &&
  466. listOfMoves[index].NewColumn == newColumn)
  467. {
  468. found = true;
  469. }
  470. }
  471. validMove = found;
  472. }
  473. catch (Exception)
  474. {
  475. DisplayErrorCode(3);
  476. }
  477. }
  478. return index;
  479. }
  480.  
  481. private static void MoveDame(string[,] board, string player, ref int newRow, ref int newColumn)
  482. {
  483. if (player == "a")
  484. {
  485. for (int i = 1; i < 8; i = i + 2)
  486. {
  487. if (board[0, i] == Space)
  488. {
  489. newColumn = i;
  490. newRow = 0;
  491. break;
  492. }
  493. }
  494. }
  495. else
  496. {
  497. for (int i = 0; i < 7; i = i + 2)
  498. {
  499. if (board[BoardSize - 1, i] == Space)
  500. {
  501. newColumn = i;
  502. newRow = BoardSize - 1;
  503. break;
  504. }
  505. }
  506. }
  507. }
  508.  
  509. private static void MovePiece(string[,] board, int[,] playersPieces,
  510. string chosenPiece, int newRow, int newColumn)
  511. {
  512. int index, currentRow, currentColumn;
  513. string player;
  514. index = Convert.ToInt32(chosenPiece.Substring(1));
  515. currentRow = playersPieces[index, Row];
  516. currentColumn = playersPieces[index, Column];
  517. board[currentRow, currentColumn] = Space;
  518. if (newRow == BoardSize - 1 && playersPieces[index, Dame] == 0)
  519. {
  520. player = "a";
  521. playersPieces[0, 1] = playersPieces[0, 1] + 1;
  522. playersPieces[index, Dame] = 1;
  523. chosenPiece = chosenPiece.ToUpper();
  524. MoveDame(board, player, ref newRow, ref newColumn);
  525. }
  526. else if (newRow == 0 && playersPieces[index, Dame] == 0)
  527. {
  528. player = "b";
  529. playersPieces[0, 1] = playersPieces[0, 1] + 1;
  530. playersPieces[index, Dame] = 1;
  531. chosenPiece = chosenPiece.ToUpper();
  532. MoveDame(board, player, ref newRow, ref newColumn);
  533. }
  534. playersPieces[index, Row] = newRow;
  535. playersPieces[index, Column] = newColumn;
  536. board[newRow, newColumn] = chosenPiece;
  537. }
  538.  
  539. private static void MakeMove(string[,] board, int[,] playersPieces, int[,] opponentsPieces, MoveRecord[] listOfMoves, int pieceIndex)
  540. {
  541. string piece, middlePiece;
  542. int newRow, newColumn, playersPieceIndex, currentRow, currentColumn;
  543. int middlePieceRow, middlePieceColumn;
  544. bool jumping;
  545. playersPieces[0, 0] = playersPieces[0, 0] + 1;
  546. if (pieceIndex > 0)
  547. {
  548. piece = listOfMoves[pieceIndex].Piece;
  549. newRow = listOfMoves[pieceIndex].NewRow;
  550. newColumn = listOfMoves[pieceIndex].NewColumn;
  551. playersPieceIndex = Convert.ToInt32(piece.Substring(1));
  552. currentRow = playersPieces[playersPieceIndex, Row];
  553. currentColumn = playersPieces[playersPieceIndex, Column];
  554. jumping = listOfMoves[pieceIndex].CanJump;
  555. MovePiece(board, playersPieces, piece, newRow, newColumn);
  556. if (jumping)
  557. {
  558. middlePieceRow = (currentRow + newRow) / 2;
  559. middlePieceColumn = (currentColumn + newColumn) / 2;
  560. middlePiece = board[middlePieceRow, middlePieceColumn];
  561. Console.WriteLine("jumped over " + middlePiece);
  562. }
  563. }
  564. }
  565.  
  566. private static string SwapPlayer(string nextPlayer)
  567. {
  568. if (nextPlayer == "a")
  569. {
  570. return "b";
  571. }
  572. else
  573. {
  574. return "a";
  575. }
  576. }
  577.  
  578. private static void PrintResult(int[,] a, int[,] b, string nextPlayer)
  579. {
  580.  
  581. Console.WriteLine("Game ended");
  582. Console.WriteLine(nextPlayer + " lost this game as they cannot make a move");
  583. PrintPlayerPieces(a, b);
  584. }
  585.  
  586. private static void Game()
  587. {
  588. int[,] A = new int[NumberOfPieces + 1, 3];
  589. int[,] B = new int[NumberOfPieces + 1, 3];
  590. string[,] board = new string[BoardSize, BoardSize];
  591. MoveRecord[] listOfMoves = new MoveRecord[MaxMoves];
  592. for (int i = 0; i < MaxMoves; i++)
  593. {
  594. MoveRecord tempRec = new MoveRecord();
  595. listOfMoves[i] = tempRec;
  596. }
  597. bool fileFound = false, gameEnd = false;
  598. string nextPlayer = "a";
  599. int pieceIndex = 0;
  600. SetUpBoard(board, A, B, ref fileFound);
  601. if (!fileFound)
  602. {
  603. gameEnd = true;
  604. }
  605. while (!gameEnd)
  606. {
  607. PrintPlayerPieces(A, B);
  608. DisplayBoard(board);
  609. Console.WriteLine("Next Player: " + nextPlayer);
  610. ClearList(listOfMoves);
  611. if (nextPlayer == "a")
  612. {
  613. ListPossibleMoves(board, A, nextPlayer, listOfMoves);
  614. if (!ListEmpty(listOfMoves))
  615. {
  616. pieceIndex = SelectMove(listOfMoves);
  617. MakeMove(board, A, B, listOfMoves, pieceIndex);
  618. nextPlayer = SwapPlayer(nextPlayer);
  619. }
  620. else
  621. {
  622. gameEnd = true;
  623. }
  624. }
  625. else
  626. {
  627. ListPossibleMoves(board, B, nextPlayer, listOfMoves);
  628. if (!ListEmpty(listOfMoves))
  629. {
  630. pieceIndex = SelectMove(listOfMoves);
  631. MakeMove(board, B, A, listOfMoves, pieceIndex);
  632. nextPlayer = SwapPlayer(nextPlayer);
  633. }
  634. else
  635. {
  636. gameEnd = true;
  637. }
  638. }
  639. }
  640. if (fileFound)
  641. {
  642. PrintResult(A, B, nextPlayer);
  643. }
  644.  
  645. Console.ReadLine();
  646. }
  647.  
  648. private static void Save(List<int> numbers)
  649. {
  650. //string player = piece[0].ToString().ToLower();
  651. using (StreamWriter writetext = new StreamWriter("AutoSave.txt"))
  652. {
  653.  
  654. numbers.ForEach(writetext.WriteLine);
  655. }
  656. }
  657.  
  658. static void Main(string[] args)
  659. {
  660. Game();
  661. }
  662. }
  663. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement