ECE497 Project BattleShip

From eLinux.org
Revision as of 06:54, 20 August 2018 by Yoder (talk | contribs) (Updating for Fall 2018)
Jump to: navigation, search

thumb‎ Embedded Linux Class by Mark A. Yoder


Team members: Dustin D. Kline, Douglas T. Wise, Sara C. Harvey-Browne

Grading Template

0 = Missing, 5=OK, 10=Wow!

00 Executive Summary 
00 Installation Instructions
00 User Instructions
00 Highlights
00 Theory of Operation
00 Work Breakdown
00 Future Work
00 Conclusions
00 Demo
00 Late
Comments: I'm looking forward to seeing this.

Score:  10/100

(Inline Comment)

Executive Summary

The purpose of this project is to establish a wireless connection between two beaglebones that will run a game of BattleShip. We will be using pygame to "play" the game and enter key and arrow keys from the computer keyboard. The direction inputs will allow the user to move their location on the board to first place ships in the directions of up, down, left, and right, and then move their location to shoot ships of their opponent. Once a player has won, there will a prompt for both players asking them if they would like to play again. If the answer is yes then the game will restart, otherwise the program is ended.


Status of Functioning Portions: Overall game-play of Battleship is fully working. Upon starting the game, the player places their ships. There is logic that does not allow them to overlap ships or wrap the ships around the board. Players take turns taking shots once both players have all five of their ships placed. When it is the players turn, they move the cursor around to where they would like to shoot, then press the 'pause' button on the Beagle to shoot. If the shot is a miss then it will turn the square yellow. If the shot is a hit then it will turn the square red. The player is not allowed to shoot a second time on a red square. At the start of a player's turn they are shown where the opponent shot in reference to where they placed their ships. Once the game is over, the winner is notified and both players are asked if they would like to continue in the terminal on their computer. If both players type in 'yes' then they are put back into the mode where they can place their five ships again. If either player types in 'no' then the program is exited. To move up, left, down, and right the player uses 4 push buttons and to "shoot" or "select" the player uses PAUSE button on the board. We are also able to connect beaglebones directly to wifi, and then create a socket between the beaglebone and another beaglebone, or between a beaglebone and a computer.



Here is a link to the Batteship wikipedia page: https://en.wikipedia.org/wiki/Battleship_(game)

Installation Instructions

BeagleboneBattleshipWiring.jpg
BeagleboneBattleshipWiring1.jpg

Here is a link to the github repository that contains all files necessary to run this project:

Below is a list of packages that need to be installed in your linux in order to run this project:

  • simplejson
  • pygame
  • python

Below is a list of hardware pieces for your beaglebone in order to run this project:

  • 4 push buttons
  • LCD screen(320x240)
  • wiring harnesses


User Instructions

Before running the Battleship files on your beaglebone blue, make sure to run both the gpio.sh and on.sh file to make sure the gpio ports are set up properly and to make sure that your LCD screen is ready to be used. If you need assistance with this portion, watch the demo video linked below in the highlights section of this wiki page. Also make sure that your beagebone is wired like it is in the images seen below.

This game can be played either on a computer or the beaglebone Blue. If you want to play the game on the computer, run the BattleShip-Desktop.py file. If you want play using the beaglebone Blue then you should run the BattleShip.py file. In order to get the game to work on the beaglebone you must run the on.sh file located under the ili9341 repository every single time you start up the beaglebone. Internet connection needs to be established before running the game unless the user wants to play on a single device. The game is started from the terminal. Upon starting the game, the user is prompted to be either 'player 1' or 'player 2'. Player 1 acts as the server and sets up a socket in python. Player 2 needs the IP address of the server (player 1) in order to connect, or 'localhost' if it's being played on a single device. In order to run this code successfully on the beaglebone Blue the device must be connected directly to the network wifi and not masquerading as your local machine. To do this, follow this link: https://github.com/beagleboard/beaglebone-blue/wiki/Frequently-Asked-Questions-(FAQ)#how-do-i-connect-beaglebone-blue-to-my-own-wifi-network and connect to the wifi of your choice. Then have player 1 run the ifconfig to get the proper IP address of the bone. The IP address to grab is next to inet under the wlan0 section once the ifconfig command is ran.

Once the connection is established, both players may place their ships. To place their ships, move the cursor around, by using the push-buttons that are wired to GP1 and UART1, to where you want to place your ship and then press the PAUSE button on the board. Then you press whichever push-button in whichever direction you would like to place your ship. You do this for all five of your ships, starting with the longest ship and moving down to the lowest ship (5, 4, 3, 3, and then 2 squares in length).

When both players have placed all of their ships, it is then player 1's turn to go first. Player 1 moves the cursor around on a now blank blue board and hits enter where that player would like to shoot. If the shot is a hit, then the square will turn red. If the shot is a miss then the square will turn yellow. After the shot, player 2 will have the screen pop up with a board showing where they placed their ships and where the opponent had shot. Then they hit enter for the screen to go back to their own board of where they are to shoot at. Player 2 moves the cursor around and hits enter where they would like to shoot. This game-play continues until someone has sank all their opponents ships.

In order to win, the game keeps track of the number of hits a player gets. Once the hit count reaches 17(number of hits it takes to sink all ships) the winner is notified. Both players are then asked if they would like to continue through a terminal prompt. 'yes' starts a new game, 'no' quits the game.

Highlights

Some key features include the beaglebone's ability to connect directly to the wifi, allowing us to connect user's together anywhere. This eliminated the need for port-forwarding and the connection to the host computer. Also, the ability to send specific objects over the wifi using simplejson was incredibly useful, as it allowed us to send only what we needed to send during gameplay. This reduced the amount of code that needed to be implemented. We were also able to display pygame on the LCD screen. Initially pygame wasn't working on the LCD, and the game was displayed on the host computer screen. However, implementing the LCD, allows us to have the Battleship game contained on the beaglebone.

We took python client-server code, which were originally two different files, and merged them into a single client-server code. Which allowed a single user to run the code as either the client or the server. This feature allowed us to keep the games code as a single file, and only one file needed to run the Battleship game after installation and setup.

Another feature was the use of buttons to control the cursor. With limited GPIO ports we converted two UART ports to allow for the usage of four pushbuttons.1

Here is a link to a helpful youtube video: https://youtu.be/OSjpHft2R4Y

Theory of Operation

Battleship is coded in python. At the beginning of the game the user is prompted to be either player 1 or player 2. Player 1 corresponds to the server and player 2 the client. The server sets up a socket in python that the client can connect to. The client needs the IP address of the server and the connection can be made.

There are three boards that the game keeps track of

  • Board: where the user places their ships
  • play_board: the board that displays during gameplay, and shows the users turn actions
  • opponent_board: the user doesn't see this board, it is updated with the play_board and is sent to the opponent to update their Board

The boards are sent back and forth through the socket using simplejson. The boards for the game are stored as 2D arrays, with strings of that squares colors being stored in the corresponding location of the board inside the array. Simplejson allows us to send the 2D arrays back and forth between the beaglebones through the socket that was established.

The game itself goes through different states of game-play. First starting with ship placement and then moving on to the main game-play loop of taking turns shooting. Then finally there is an end-game loop that tells the person who won and lost while also asking the players if they would like to replay. If yes, then it sets all three boards for both players back to straight blue and also resets the hit counter for both players.

Pygame events are used to handle the controls of the game. Keyboard keys and gpio pushbuttons are registered in the event queue, and handled one at a time in order. The action button also acts as a signal to change the game state.

While the game is played a hitCount is kept track of, the number of hits needed to win is 17. This check happens after every turn, and if 17 is reached the winner is notified on screen. Before each turn, the game checks to see if the user has lost, by checking the user's board, that has been updated with the opponent's move, for any remaining ship markers(grey tiles). If no grey tiles are left, then that indicates a loss by the user. If they users choose to continue playing, the hitCount and boards are all reset and the game loops back to the beginning, if not it closes.

Work Breakdown

Douglas:

  • Client-server code
  • Networking the game(integration of client-server code)
  • 2 player functionality
  • Screen drawing/updating(player moves)
  • Ship placement
  • Game-play
  • Win checks
  • Post game restart/quit
  • Wiki page
  • BeagleBone wifi connectivity

Dustin:

  • Integrate client-server code into Battleship code
  • Initial Battleship code that worked on one computer
  • Got Battleship code working with pygames on LCD display
  • 2 player functionality
  • Get text to display on screen over the board
  • Win checks
  • Game-play
  • Get beaglebone Blue directly connected to school network
  • Filled out wiki page
  • Youtube demo video


Sara:

  • Project idea
  • Ship placement where none of the ships will overlap.
  • Using two GPIO ports for pushbuttons
  • Getting UART ports to act as GPIO ports to allow for extra push-buttons (with Dr. Yoders help)

Future Work

  • Have Battleship code run right on beaglebone start-up
  • Create some sort of hardware case for the beaglebone so there is only buttons and LCD display available
  • Keep track of highscore (least amount of shots to win)
  • Get rotary encoders to work as cursor input


Conclusions

Overall this was a fun project, we learned a lot about the beaglebones and Linux. It was a fun challenge to get everything working together on the beaglebone. To make the project more interesting, we could have looked into adding animations, different control options, or even adding our own rules to the Battleship game.




thumb‎ Embedded Linux Class by Mark A. Yoder