Battleship

Keywords: battleship game, Python, pygame, data abstraction, pattern recognition, algorithm design

This project is hosted on GitHub. It was done as part of the requirements for the module CZ1003 Introduction to Computational Thinking at NTU, along with my group members Tay Quan Rui and Teh Siang Wen.

The implementation of Battleship on Python is done using the Pygame library. Players must create an account and login before they can play. The game itself consists of two stages: first is the ship placement ship, where the player can place their ships on their grid, and next is the actual game. The player and the computer take turns “attacking” the opponent’s grid. Moves for the computer are randomly generated. The game ends once all the ships on either side are tagged.

To develop the complete game, I started with decomposing the problem into sub-problems: account creation, account login, pre-game configuration, and gameplay.

Account creation and account login required data abstraction and pattern recognition. The implementation of a button was not what one thinks of a button intuitively: the actual implementation involved coding the button to change colour (get highlighted) when the cursor was in a certain location to signal hovering over the button, which was abstracted in the actual interface. There was also a pattern to the implementation: the options were mutually exclusive, and incorrect input required re-entering.

In the pre-game configuration, I made use of data abstraction for representing the game board. Although the user got a visual of the board with “X”s for grids that were attacked, the actual implementation of it was actually a three dimensional array with dimensions (depth, row, col), where a depth of 1 indicated the user’s grid a depth of 0 indicated the computer’s grid.

The gameplay was essentially a loop, where the turn alternated between the user and the computer till the game ended. At the end of each turn, the program checked whether all the ships on one side were market, and if they weren’t, continued the game. Once they were, the game would be ended and the player would either get a “You Win” or “You Lose” message.


Previous
Previous

Predicting Credit Card Churn