Skip to content

Pa2 bit board storage #9

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 55 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# Prerequisites
*.d

# Object files
*.o
*.ko
*.obj
*.elf

# Linker output
*.ilk
*.map
*.exp

# Precompiled Headers
*.gch
*.pch

# Libraries
*.lib
*.a
*.la
*.lo

# Shared objects (inc. Windows DLLs)
*.dll
*.so
*.so.*
*.dylib

# Executables
*.exe
*.out
*.app
*.i*86
*.x86_64
*.hex

# Debug files
*.dSYM/
*.su
*.idb
*.pdb

# Kernel Module Compile Results
*.mod*
*.cmd
.tmp_versions/
modules.order
Module.symvers
Mkfile.old
dkms.conf

.idea/*
cmake-build-debug/*
9 changes: 9 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[submodule "cereal"]
path = cereal
url = [email protected]:USCiLab/cereal.git
[submodule "googletest"]
path = googletest
url = [email protected]:google/googletest.git
[submodule "dtl"]
path = dtl
url = [email protected]:cubicdaiya/dtl.git
15 changes: 15 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
cmake_minimum_required(VERSION 3.6)

project(Battleship)
enable_language(ASM_NASM)

set(CMAKE_CXX_STANDARD 14)

add_subdirectory(googletest)

add_subdirectory(src)

add_subdirectory(test)



80 changes: 79 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,79 @@
CSCI_366_Programming_Assignments
# CSCI 466 PA2 - Bit Board Storage

## Instructions


Complete the following assignment individually.
Submit your work on D2L into the "Programming Assignment 2" folder.


## Learning Objectives

In this programming assignment you will:

- Implement a class to store the setup board as bits. The core functionality of your class will be implemented in the
Netwide Assembler (NASM).


## Overview

Your objective is to extend your basic implementation of the
[Battleship](https://en.wikipedia.org/wiki/Battleship_\(game\))
game to store the player setup boards in two dimensional bit arrays.
The core of the idea is that the `Server` reads in the player setup boards and instead of referencing the file to
evaluate each shot, it saves the board in internal memory.
You might wonder why not store the board in a two dimensional vector?
Well, since all we want to store is whether a ship is present in a given position, using a vector, even just of bytes, still uses 8 bits for every ship position.
We want to get this down to one bit.
Of course you could use something like an array of [bitsets](http://www.cplusplus.com/reference/bitset/bitset/), but
you can save even more memory by implementing a two dimensional bit storage yourself in assembly.
For very large data sets, or on very memory-constrained devices, every bit counts!

To complete this assignment you will extend your current implementation of Battleship.
I have added some functions that you'll need to implement, but most of your `Server` code should carry over.
Your `Client` code will remain unchanged.
I have also added a few tests to check the new functionality, while keeping all the old tests in tact.
So, if you have those working, great!
If not, you can earn back some points by getting your code to pass the tests you missed in PA1.


## Program Invocation

To play Battleship you should first start the server by running the `run-server` executable.
Then start the player clients by running two instances of the `run-client` executable.
Unfortunately, the client and server executables will not do anything interesting until you implement `Client.cpp`
and `Server.cpp`.
As you progress in these implementations, your code will pass more and more tests in `tests.cpp`.
When your code passes all the tests, you will be able to run the client and server executables to play the game.


## Bonus

I will award __one bonus point__ for each of the following:

* Server sends a different result when a ship is sunk. The client implementation notifies the player when they sink a
ship (implementation and tests).
This will require you to store more than 1 bit in the setup board array - you will also need to store ship type.


## What to Submit

Submit your work on D2L into the "Programming Assignment 2" folder.

* A text file containing a link to a GitHub repository with your solution for the base assignment.
Please not that your repository should be private, so that other students cannot see your solutions.
You can create an unlimited number of private repositories by upgrading your github account to the pro version for
free using the [academic discount](https://help.github.com/en/github/teaching-and-learning-with-github-education/applying-for-an-educator-or-researcher-discount) with your school email.
In order for us to be able to see your solution, please add github users `cooperstrahan` and `mwittie` as
collaborators through repository settings.

* If you implement the bonus solution, please submit a YouTube video of your implementation.
Be sure to show the tests you have written as well as your assembly code.


## Grading

We will grade your submissions based on how many test cases in `tests.cpp` your solution passes.



1 change: 1 addition & 0 deletions cereal
Submodule cereal added at a5a309
1 change: 1 addition & 0 deletions dtl
Submodule dtl added at 6b030d
1 change: 1 addition & 0 deletions googletest
Submodule googletest added at 8b4817
Binary file added images/system_architecture.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
10 changes: 10 additions & 0 deletions player_1.setup_board.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
CCCCC_____
BBBB______
RRR_______
SSS_______
D_________
D_________
__________
__________
__________
__________
10 changes: 10 additions & 0 deletions player_2.setup_board.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
C________D
C_________
C_________
C_________
CBBBB_____
_______RRR
_________S
_________S
_________S
D_________
38 changes: 38 additions & 0 deletions src/BitArray2D.asm
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@

global set_bit_elem
global get_bit_elem
section .text

set_bit_elem:
push rbp ; save the base pointer on the stack (at rsp+8)
mov rbp, rsp ; set up the rbp for the bottom of this frame

; rdi contains array pointer
; rsi contains row width
; rdx contains row
; rcx contains col

; add your code here

mov rsp, rbp ; restore stack pointer to before we pushed parameters onto the stack
pop rbp ; remove rbp from the stack to restore rsp to initial value
ret ; return value in rax




get_bit_elem:
push rbp ; save the base pointer on the stack (at rsp+8)
mov rbp, rsp ; set up the rbp for the bottom of this frame

; rdi contains array pointer
; rsi contains row width
; rdx contains row
; rcx contains col

; add your code here - for now returning 0
mov rax, 0

mov rsp, rbp ; restore stack pointer to before we pushed parameters onto the stack
pop rbp ; remove rbp from the stack to restore rsp to initial value
ret ; return value in rax
44 changes: 44 additions & 0 deletions src/BitArray2D.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// Battleship game assignment for MSU CSCI 366
// Copyright (C) 2020 Mike P. Wittie
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.

#include <math.h>
#include "BitArray2D.hpp"

BitArray2D::BitArray2D(unsigned int rows, unsigned int columns) {

}


BitArray2D::~BitArray2D() {

}


bool BitArray2D::get(unsigned int row, unsigned int column){
// check array bounds

// get the element
return get_bit_elem(array, columns, row, column);
}



void BitArray2D::set(unsigned int row, unsigned int column){
// check array bounds

// set the element
set_bit_elem(array, columns, row, column);
}
110 changes: 110 additions & 0 deletions src/BitArray2D.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
// Battleship game assignment for MSU CSCI 366
// Copyright (C) 2020 Mike P. Wittie
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.



#include <iostream>
#include <exception>

#ifndef BATTLESHIP_BITARRAY2D_HPP
#define BATTLESHIP_BITARRAY2D_HPP

using namespace std;

class BitArray2DException: public exception
{
private:
char *cstr;

public:
BitArray2DException(string message){
cstr = new char[message.size() + 1];
message.copy(cstr, message.size() + 1);
cstr[message.size()] = '\0';
}

~BitArray2DException(){
delete cstr;
}

virtual const char* what() const throw(){
return cstr;
}
};


/**
* Sets a bit in a two dimensional bit array
* @param array - pointer to a bit array
* @param row_width - the number of bits in a row of the two dimensional array
* @param row - row index (0 indexed)
* @param col - column index (0 indexed)
*/
extern "C" void set_bit_elem(char *array, unsigned int row_width, unsigned int row, unsigned int col);

/**
* Gets a bit in a two dimensional bit array
* @param array - pointer to a bit array
* @param row_width - the number of bits in a row of the two dimensional array
* @param row - row index (0 indexed)
* @param col - column index (0 indexed)
* @return a boolean containing the bit at [row][column]
*/
extern "C" bool get_bit_elem(char *array, unsigned int row_width, unsigned int row, unsigned int col);


class BitArray2D {
private:
/**
* The 'char' types does not mean that each bit is stored as a character - we just need the pointer
* to have some type since there is no type for bit.
*/
char* array = nullptr;

unsigned int rows;
unsigned int columns;

public:
/**
* Sets up the array to store rows * columns bits
* @param rows - number of rows
* @param columns - number of columns
*/
BitArray2D(unsigned int rows, unsigned int columns);

/**
* Deallocate memory used for array
*/
~BitArray2D();

/**
* Get bit at row and column
* @param row
* @param column
* @return bit at row and column as bool
*/
bool get(unsigned int row, unsigned int column);

/**
* Set bit to true at row and column
* @param row
* @param column
*/
void set(unsigned int row, unsigned int column);
};


#endif //BATTLESHIP_BIT_ARRAY_HPP
Loading