I'm trying to write a small program that can read and write 12 bit. The Input shouldn't have any issues but I'll include it so you understand the problem better. The input should be created as sample.lzw by the OFStream12Bits.cpp/main.cpp included below and the output should be reading the sample.lzw back from the write functions. I'm having problems with the output and getting code mismatch in the main when reading the code. I think the issues is from the operator>> and the readBit functions not sure exactly though.
Thank you very much for any help, I've been stuck on this for awhile!
The instructions for the readbit are as follows...
//basic readBit
//read12Bits(): 12Bit =
//declare Result : 12Bit = 0;
//for i = 1 to 12
//do
//declare lBit : Bit = get bit from input
//if(lBit == 1)
//then Result = (1 << (i-1)) + Result; //set bit at index i
//od
//return result
The part I don't understand is I need to return *this but there is no + operator so I can't use result to set the bit at index i. at the moment I have the code like this.
IFStream12Bits& IFStream12Bits::operator>>(int& a12BitValue)
{
//int Result = a12BitValue;
//a12BitValue = ((a12BitValue & 0x0fff) << 1);
a12BitValue = a12BitValue & 0x0fff;
for (int i = 0; i < 12; i++)
{
int bit = readBit();
if (bit == 1)
{
a12BitValue = (1 << (i - 1)) + a12BitValue; //set bit at index i
}
}
return *this;
}
Also The instructions for the readBit are as follows...
//implements mapping process. returns 0 or 1 depending on value of fBuffer[fByteIndex] & (1 << (fBitIndex - 1))
//see how it works with experiments
//at start check if (fByteCount == 0){reload();} then use reload() called as buffer does not contain any data before calling reload
//next fetch the bit store and then advance fByteIndex and fBitIndex
//if fBitIndex(highest to lowest) reaches 0 you need to switch to the next byte in the buffer. and also decrment fByteCount
//then finally return result
And the code is
int IFStream12Bits::readBit()
{
if (fByteCount == 0){ reload(); }
//int bit = fBuffer[fByteIndex] & (1 << (fBitIndex - 1));
int bit = fBuffer[fByteIndex] & (1 << (fBitIndex - 1));
int result = 0;
cout << "bit: " << bit << endl;
//added this just cause
if (bit == 0)
{
result = 0;
}
else
{
result = 1;
}
//additional logic required?
fByteIndex++;
fBitIndex--;
//switch to next byte in the buffer
if (fBitIndex == 0)
{
fByteCount--;
fBitIndex = 8;
fByteIndex = 0;
}
return result;
}
Here are the full .cpp files if you need to understand what is happening...
IFStream12Bits.cpp
#include "IFStream12Bits.h"
#include <iostream>
using namespace std;
//default constructor
IFStream12Bits::IFStream12Bits()
{
init();
}
//takes aFIleName
IFStream12Bits::IFStream12Bits(const char* aFileName)
{
init();
open(aFileName);
}
//deconstructor
IFStream12Bits::~IFStream12Bits()
{
close();
}
//initialize the integer member variables with sensible values
//:fBuffer(), fByteCount(0), fByteIndex(0), fBitIndex(8)
//fBitIndex(highToLow)
void IFStream12Bits::init()
{
for (int i = 0; i < 32; i++)
{
fBuffer[i] = 0;
}
fByteCount = 0;
fByteIndex = 0;
fBitIndex = 8;
}
//fills input buffer fBuffer with the next 32 bytes and sets fByteCount to number of bytes read
void IFStream12Bits::reload()
{
//fills fBuffer with 32 bytes
fIStream.read((char*)fBuffer, 32);
//fIStream.read((char*)fBuffer, fByteIndex + (fBitIndex % 8 ? 1 : 0));
//sets fByteCount to number of bytes read
fByteCount = fIStream.gcount();
}
//implements mapping process. returns 0 or 1 depending on value of fBuffer[fByteIndex] & (1 << (fBitIndex - 1))
//see how it works with experiments
//at start check if (fByteCount == 0){reload();} then use reload() called as buffer does not contain any data before calling reload
//next fetch the bit store and then advance fByteIndex and fBitIndex
//if fBitIndex(highest to lowest) reaches 0 you need to switch to the next byte in the buffer. and also decrment fByteCount
//then finally return result
int IFStream12Bits::readBit()
{
if (fByteCount == 0){ reload(); }
//int bit = fBuffer[fByteIndex] & (1 << (fBitIndex - 1));
int bit = fBuffer[fByteIndex] & (1 << (fBitIndex - 1));
int result = 0;
cout << "bit: " << bit << endl;
if (bit == 0)
{
result = 0;
}
else
{
result = 1;
}
//additional logic required?
fByteIndex++;
fBitIndex--;
//switch to next byte in the buffer
if (fBitIndex == 0)
{
fByteCount--;
fBitIndex = 8;
fByteIndex = 0;
}
return result;
}
void IFStream12Bits::open(const char* aFileName)
{
fIStream.open(aFileName, std::fstream::binary);
}
void IFStream12Bits::close()
{
fIStream.close();
}
bool IFStream12Bits::fail()
{
return fIStream.fail();
}
//true if no bytes left in input stream (fByteCount == 0)(should be zero if never read anythign from fIStream)
bool IFStream12Bits::eof()
{
return fByteCount == 0;
}
//read 12Bit codes from the bit input stream implements the read12Bits algorithm as shown in the tutorial
//basic readBit
//read12Bits(): 12Bit =
//declare Result : 12Bit = 0;
//for i = 1 to 12
//do
//declare lBit : Bit = get bit from input
//if(lBit == 1)
//then Result = (1 << (i-1)) + Result; //set bit at index i
//od
//return result
// multiply values by 2 to shift left???????????
IFStream12Bits& IFStream12Bits::operator>>(int& a12BitValue)
{
//int Result = a12BitValue;
//a12BitValue = ((a12BitValue & 0x0fff) << 1);
a12BitValue = a12BitValue & 0x0fff;
for (int i = 0; i < 12; i++)
{
int bit = readBit();
if (bit == 1)
{
a12BitValue = (1 << (i - 1)) + a12BitValue; //set bit at index i
}
}
return *this;
}
OFStream12Bits.cpp
#include "OFStream12Bits.h"
OFStream12Bits::OFStream12Bits()
{
init();
}
OFStream12Bits::OFStream12Bits(const char* aFileName)
{
init();
open(aFileName);
}
OFStream12Bits::~OFStream12Bits()
{
close();
}
void OFStream12Bits::init()
{
for (int i = 0; i < 32; i++)
{
fBuffer[i] = 0;
}
fByteIndex = 0;
fBitIndex = 8;
}
void OFStream12Bits::writeBit0()
{
fBitIndex--;
finishWriteBit();
}
void OFStream12Bits::writeBit1()
{
fBuffer[fByteIndex] += 1 << (fBitIndex - 1);
fBitIndex--;
finishWriteBit();
}
void OFStream12Bits::finishWriteBit()
{
if (fBitIndex == 0)
{
if (fByteIndex == 31)
{
fByteIndex++;
//write full buffer to stream
flush();
}
else
{
fByteIndex++;
fBitIndex = 8;
}
}
}
void OFStream12Bits::open(const char* aFileName)
{
fOStream.open(aFileName, std::ofstream::binary);
}
bool OFStream12Bits::fail()
{
return fOStream.fail();
}
void OFStream12Bits::close()
{
flush();
fOStream.close();
}
void OFStream12Bits::flush()
{
// do we need to add last byte?
fOStream.write((char*)fBuffer, fByteIndex + (fBitIndex % 8 ? 1 : 0));
init();
}
OFStream12Bits& OFStream12Bits::operator<<(int a12BitValue)
{
a12BitValue = a12BitValue & 0x0fff; // mask 12 lower bits
for (int i = 0; i < 12; i++) //write 12 bits
{
if (a12BitValue & 0x01) // the current lowest bit is set
{
writeBit1();
}
else
{
writeBit0();
}
a12BitValue >>= 1; // code = code / 2 --shifting value accross
}
return *this;
}
main.cpp
#include "OFStream12Bits.h"
#include "IFStream12Bits.h"
#include <iostream>
using namespace std;
void write4096()
{
cout << "Write 4096 codes" << endl;
OFStream12Bits lWriter("sample.lzw");
if (lWriter.fail())
{
cerr << "Error: unable to open output file" << endl;
exit(1);
}
for (int i = 4096; i >= 0; i--)
{
lWriter << i;
}
}
void read4096()
{
cout << "Read 4096 codes" << endl;
IFStream12Bits lInput("sample.lzw");
if (lInput.fail())
{
cerr << "Error: unable to open input file!" << endl;
exit(2);
}
for (int i = 4095; i >= 0; i--)
{
int l12BitValue;
lInput >> l12BitValue;
if (l12BitValue != i)
{
cerr << "Error: Code mismatch: " << l12BitValue << " != " << i << endl;
exit(3);
}
}
if (!lInput.eof())
{
cerr << "Error: Input stream not exhausted" << endl;
}
}
int main()
{
write4096();
read4096();
cout << "SUCCESS" << endl;
return 0;
}
Your input code is starting with the previous value. You should start with 0, because you're not clearing bits that aren't set.
IFStream12Bits& IFStream12Bits::operator>>(int& a12BitValue)
{
a12BitValue = 0;
for (int i = 0; i < 12; i++)
{
int bit = readBit();
if (bit == 1)
{
a12BitValue = (1 << (i - 1)) + a12BitValue; //set bit at index i
}
}
return *this;
}
Also, + will work here, but it's clearer to use bitwise operations when dealing with bits. Additionally, I think your shift is off. I would write the set bit line like this:
a12BitValue |= 1 << i;
If you think about it, when i is 0, you want to set the first bit (which is 1 or 1 << 0.) When i is 1, you want the next bit, and so on. So you should not need to subtract one.
I'm not sure that's the only problem, but you might try testing each class independently with unit tests. For example, start with a raw byte buffer, like {0x89, 0xAB, 0xCD, 0xEF, 0x01}, and then read three sets of 12 bits off. Verify they are correct. Then create an empty buffer, and write specific bits to it, and check that the bytes are correct.
By testing the algorithms independently, and with very strict input/output, you'll find it much easier to determine the flaw.
Related
I am working on makingthis connect 4 game to be modular with different grid sizes from 3x3 up to a 10x10 as well as a modular amount of winning "pucks". The program below works by passing 3 arguments which is the grid size (grid is square), the continuous amount of pucks needed to win, and who starts first (not implemented yet). So the command to run it would be connectM 6 5 1 for example.
On the code below you will see that attempt. The program works well when you use 4 as the second argument but anything above it and I am getting a segmentation fault around line 338 and I can't put my finger on it. Does anyone have any insight on something I am obviously doing wrong?
#include <stdio.h>
#include <iostream>
#include <vector>
#include <limits.h>
#include <array>
#include <sstream>
#define min(a,b) (((a) < (b)) ? (a) : (b))
#define max(a,b) (((a) > (b)) ? (a) : (b))
using namespace std;
// function declarations
void printBoard(vector<vector<int> >&);
int userMove();
void makeMove(vector<vector<int> >&, int, unsigned int);
void errorMessage(int);
int aiMove();
vector<vector<int> > copyBoard(vector<vector<int> >);
bool winningMove(vector<vector<int> >&, unsigned int);
int scoreSet(vector<unsigned int>, unsigned int);
int tabScore(vector<vector<int> >, unsigned int);
array<int, 2> miniMax(vector<vector<int> >&, unsigned int, int, int, unsigned int);
int heurFunction(unsigned int, unsigned int, unsigned int);
// Avoid magic numbers
unsigned int NUM_COL = 7; // how wide is the board
unsigned int NUM_ROW = 7; // how tall
unsigned int PLAYER = 1; // player number
unsigned int COMPUTER = 2; // AI number
unsigned int MAX_DEPTH = 5; // the default "difficulty" of the computer controlled AI
unsigned int WINNING_PUCKS = 4; //Default winning pucks
unsigned int FIRST_PLAYER = 0;
bool gameOver = false; // flag for if game is over
unsigned int turns = 0; // count for # turns
unsigned int currentPlayer = PLAYER; // current player
vector<vector<int>> board(NUM_ROW, vector<int>(NUM_COL)); // the game board
/**
* game playing function
* loops between players while they take turns
*/
void playGame() {
printBoard(board); // print initial board
while (!gameOver) { // while no game over state
if (currentPlayer == COMPUTER) { // AI move
makeMove(board, aiMove(), COMPUTER);
}
else if (currentPlayer == PLAYER) { // player move
makeMove(board, userMove(), PLAYER);
}
else if (turns == NUM_ROW * NUM_COL) { // if max number of turns reached
gameOver = true;
}
gameOver = winningMove(board, currentPlayer); // check if player won
currentPlayer = (currentPlayer == 1) ? 2 : 1; // switch player
turns++; // iterate number of turns
cout << endl;
printBoard(board); // print board after successful move
}
if (turns == NUM_ROW * NUM_COL) { // if draw condition
cout << "Draw!" << endl;
}
else { // otherwise, someone won
cout << ((currentPlayer == PLAYER) ? "AI Wins!" : "Player Wins!") << endl;
}
}
/**
* function that makes the move for the player
* #param b - the board to make move on
* #param c - column to drop piece into
* #param p - the current player
*/
void makeMove(vector<vector<int> >& b, int c, unsigned int p) {
// start from bottom of board going up
for (unsigned int r = 0; r < NUM_ROW; r++) {
if (b[r][c] == 0) { // first available spot
b[r][c] = p; // set piece
break;
}
}
}
/**
* prompts the user for their move
* and ensures valid user input
* #return the user chosen column
*/
int userMove() {
int move = -1; // temporary
while (true) { // repeat until proper input given
cout << "Enter a column: ";
cin >> move; // init move as input
if (!cin) { // if non-integer
cin.clear();
cin.ignore(INT_MAX, '\n');
errorMessage(1); // let user know
}
else if (!((unsigned int)move < NUM_COL && move >= 0)) { // if out of bounds
errorMessage(2); // let user know
}
else if (board[NUM_ROW - 1][move] != 0) { // if full column
errorMessage(3); // let user know
}
else { // if it gets here, input valid
break;
}
cout << endl << endl;
}
return move;
}
/**
* AI "think" algorithm
* uses minimax to find ideal move
* #return - the column number for best move
*/
int aiMove() {
cout << "AI is thinking about a move..." << endl;
return miniMax(board, MAX_DEPTH, 0 - INT_MAX, INT_MAX, COMPUTER)[1];
}
/**
* Minimax implementation using alpha-beta pruning
* #param b - the board to perform MM on
* #param d - the current depth
* #param alf - alpha
* #param bet - beta
* #param p - current player
*/
array<int, 2> miniMax(vector<vector<int> > &b, unsigned int d, int alf, int bet, unsigned int p) {
/**
* if we've reached minimal depth allowed by the program
* we need to stop, so force it to return current values
* since a move will never (theoretically) get this deep,
* the column doesn't matter (-1) but we're more interested
* in the score
*
* as well, we need to take into consideration how many moves
* ie when the board is full
*/
if (d == 0 || d >= (NUM_COL * NUM_ROW) - turns) {
// get current score to return
return array<int, 2>{tabScore(b, COMPUTER), -1};
}
if (p == COMPUTER) { // if AI player
array<int, 2> moveSoFar = {INT_MIN, -1}; // since maximizing, we want lowest possible value
if (winningMove(b, PLAYER)) { // if player about to win
return moveSoFar; // force it to say it's worst possible score, so it knows to avoid move
} // otherwise, business as usual
for (unsigned int c = 0; c < NUM_COL; c++) { // for each possible move
if (b[NUM_ROW - 1][c] == 0) { // but only if that column is non-full
vector<vector<int> > newBoard = copyBoard(b); // make a copy of the board
makeMove(newBoard, c, p); // try the move
int score = miniMax(newBoard, d - 1, alf, bet, PLAYER)[0]; // find move based on that new board state
if (score > moveSoFar[0]) { // if better score, replace it, and consider that best move (for now)
moveSoFar = {score, (int)c};
}
alf = max(alf, moveSoFar[0]);
if (alf >= bet) { break; } // for pruning
}
}
return moveSoFar; // return best possible move
}
else {
array<int, 2> moveSoFar = {INT_MAX, -1}; // since PLAYER is minimized, we want moves that diminish this score
if (winningMove(b, COMPUTER)) {
return moveSoFar; // if about to win, report that move as best
}
for (unsigned int c = 0; c < NUM_COL; c++) {
if (b[NUM_ROW - 1][c] == 0) {
vector<vector<int> > newBoard = copyBoard(b);
makeMove(newBoard, c, p);
int score = miniMax(newBoard, d - 1, alf, bet, COMPUTER)[0];
if (score < moveSoFar[0]) {
moveSoFar = {score, (int)c};
}
bet = min(bet, moveSoFar[0]);
if (alf >= bet) { break; }
}
}
return moveSoFar;
}
}
/**
* function to tabulate current board "value"
* #param b - the board to evaluate
* #param p - the player to check score of
* #return - the board score
*/
int tabScore(vector<vector<int> > b, unsigned int p) {
int score = 0;
vector<unsigned int> rs(NUM_COL);
vector<unsigned int> cs(NUM_ROW);
vector<unsigned int> set(WINNING_PUCKS);
/**
* horizontal checks, we're looking for sequences of WINNING_PUCKS
* containing any combination of AI, PLAYER, and empty pieces
*/
for (unsigned int r = 0; r < NUM_ROW; r++) {
for (unsigned int c = 0; c < NUM_COL; c++) {
rs[c] = b[r][c]; // this is a distinct row alone
}
for (unsigned int c = 0; c < NUM_COL - (WINNING_PUCKS - 1); c++) {
for (int i = 0; i < WINNING_PUCKS; i++) {
set[i] = rs[c + i]; // for each possible "set" of WINNING_PUCKS spots from that row
}
score += scoreSet(set, p); // find score
}
}
// vertical
for (unsigned int c = 0; c < NUM_COL; c++) {
for (unsigned int r = 0; r < NUM_ROW; r++) {
cs[r] = b[r][c];
}
for (unsigned int r = 0; r < NUM_ROW - (WINNING_PUCKS - 1); r++) {
for (int i = 0; i < WINNING_PUCKS; i++) {
set[i] = cs[r + i];
}
score += scoreSet(set, p);
}
}
// diagonals
for (unsigned int r = 0; r < NUM_ROW - (WINNING_PUCKS - 1); r++) {
for (unsigned int c = 0; c < NUM_COL; c++) {
rs[c] = b[r][c];
}
for (unsigned int c = 0; c < NUM_COL - (WINNING_PUCKS - 1); c++) {
for (int i = 0; i < WINNING_PUCKS; i++) {
set[i] = b[r + i][c + i];
}
score += scoreSet(set, p);
}
}
for (unsigned int r = 0; r < NUM_ROW - (WINNING_PUCKS - 1); r++) {
for (unsigned int c = 0; c < NUM_COL; c++) {
rs[c] = b[r][c];
}
for (unsigned int c = 0; c < NUM_COL - (WINNING_PUCKS - 1); c++) {
for (int i = 0; i < WINNING_PUCKS; i++) {
set[i] = b[r + WINNING_PUCKS - 1 - i][c + i];
}
score += scoreSet(set, p);
}
}
return score;
}
/**
* function to find the score of a set of WINNING_PUCKS spots
* #param v - the row/column to check
* #param p - the player to check against
* #return - the score of the row/column
*/
int scoreSet(vector<unsigned int> v, unsigned int p) {
unsigned int good = 0; // points in favor of p
unsigned int bad = 0; // points against p
unsigned int empty = 0; // neutral spots
for (unsigned int i = 0; i < v.size(); i++) { // just enumerate how many of each
good += (v[i] == p) ? 1 : 0;
bad += (v[i] == PLAYER || v[i] == COMPUTER) ? 1 : 0;
empty += (v[i] == 0) ? 1 : 0;
}
// bad was calculated as (bad + good), so remove good
bad -= good;
return heurFunction(good, bad, empty);
}
/**
* """heuristic function"""
* #param g - good points
* #param b - bad points
* #param z - empty spots
* #return - the score as tabulated
*/
// int heurFunction(unsigned int g, unsigned int b, unsigned int z) {
// int score = 0;
// if (g == 4) { score += 500001; } // preference to go for winning move vs. block
// else if (g == 3 && z == 1) { score += 5000; }
// else if (g == 2 && z == 2) { score += 500; }
// else if (b == 2 && z == 2) { score -= 501; } // preference to block
// else if (b == 3 && z == 1) { score -= 5001; } // preference to block
// else if (b == 4) { score -= 500000; }
// return score;
// }
int heurFunction(unsigned int g, unsigned int b, unsigned int z) {
int score = 0;
if (g == WINNING_PUCKS) { score += 500001; } // preference to go for winning move vs. block
else if (g > z) { score += 5000; }
else if (g == z) { score += 500; }
else if (b == z) { score -= 501; } // preference to block
else if (b > z) { score -= 5001; } // preference to block
else if (b == WINNING_PUCKS) { score -= 500000; }
return score;
}
/**
* function to determine if a winning move is made
* #param b - the board to check
* #param p - the player to check against
* #return - whether that player can have a winning move
*/
bool winningMove(vector<vector<int> > &b, unsigned int p) {
unsigned int winSequence = 0; // to count adjacent pieces
// for horizontal checks
for (unsigned int c = 0; c < NUM_COL - (WINNING_PUCKS - 1); c++) { // for each column
for (unsigned int r = 0; r < NUM_ROW; r++) { // each row
for (int i = 0; i < WINNING_PUCKS; i++) { // recall you need WINNING_PUCKS to win
if ((unsigned int)b[r][c + i] == p) { // if not all pieces match
winSequence++; // add sequence count
}
if (winSequence == WINNING_PUCKS) { return true; } // if WINNING_PUCKS in row
}
winSequence = 0; // reset counter
}
}
// vertical checks
for (unsigned int c = 0; c < NUM_COL; c++) {
for (unsigned int r = 0; r < NUM_ROW - (WINNING_PUCKS - 1); r++) {
for (int i = 0; i < WINNING_PUCKS; i++) {
if ((unsigned int)b[r + i][c] == p) {
winSequence++;
}
if (winSequence == WINNING_PUCKS) { return true; }
}
winSequence = 0;
}
}
// the below two are diagonal checks
for (unsigned int c = 0; c < NUM_COL - (WINNING_PUCKS - 1); c++) {
for (unsigned int r = 3; r < NUM_ROW; r++) {
for (int i = 0; i < WINNING_PUCKS; i++) {
if ((unsigned int)b[r - i][c + i] == p) {
winSequence++;
}
if (winSequence == WINNING_PUCKS) { return true; }
}
winSequence = 0;
}
}
for (unsigned int c = 0; c < NUM_COL - (WINNING_PUCKS - 1); c++) {
for (unsigned int r = 0; r < NUM_ROW - (WINNING_PUCKS - 1); r++) {
for (int i = 0; i < WINNING_PUCKS; i++) {
if ((unsigned int)b[r + i][c + i] == p) {
winSequence++;
}
if (winSequence == WINNING_PUCKS) { return true; }
}
winSequence = 0;
}
}
return false; // otherwise no winning move
}
/**
* sets up the board to be filled with empty spaces
* also used to reset the board to this state
*/
void initBoard() {
for (unsigned int r = 0; r < NUM_ROW; r++) {
for (unsigned int c = 0; c < NUM_COL; c++) {
board[r][c] = 0; // make sure board is empty initially
}
}
}
/**
* function to copy board state to another 2D vector
* ie. make a duplicate board; used for mutating copies rather
* than the original
* #param b - the board to copy
* #return - said copy
*/
vector<vector<int> > copyBoard(vector<vector<int> > b) {
vector<vector<int>> newBoard(NUM_ROW, vector<int>(NUM_COL));
for (unsigned int r = 0; r < NUM_ROW; r++) {
for (unsigned int c = 0; c < NUM_COL; c++) {
newBoard[r][c] = b[r][c]; // just straight copy
}
}
return newBoard;
}
/**
* prints the board to console out
* #param b - the board to print
*/
void printBoard(vector<vector<int> > &b) {
for (unsigned int i = 0; i < NUM_COL; i++) {
cout << " " << i;
}
cout << endl << "---------------" << endl;
for (unsigned int r = 0; r < NUM_ROW; r++) {
for (unsigned int c = 0; c < NUM_COL; c++) {
cout << "|";
switch (b[NUM_ROW - r - 1][c]) {
case 0: cout << " "; break; // no piece
case 1: cout << "O"; break; // one player's piece
case 2: cout << "X"; break; // other player's piece
}
if (c + 1 == NUM_COL) { cout << "|"; }
}
cout << endl;
}
cout << "---------------" << endl;
cout << endl;
}
/**
* handler for displaying error messages
* #param t - the type of error to display
*/
void errorMessage(int t) {
if (t == 1) { // non-int input
cout << "Use a value 0.." << NUM_COL - 1 << endl;
}
else if (t == 2) { // out of bounds
cout << "That is not a valid column." << endl;
}
else if (t == 3) { // full column
cout << "That column is full." << endl;
}
cout << endl;
}
/**
* main driver
*/
int main(int argc, char** argv) {
// int i = -1; bool flag = false;
// if (argc == 2) {
// istringstream in(argv[1]);
// if (!(in >> i)) { flag = true; }
// if (i > (int)(NUM_ROW * NUM_COL) || i <= -1) { flag = true; }
// if (flag) { cout << "Invalid command line argument, using default depth = 5." << endl; }
// else { MAX_DEPTH = i; }
// }
if(argc <= 1){
cout << "No arguments fed. Terminating";
return 0;
}
if(argc == 4){
int gridSize = atoi(argv[1]);
int diskAmount = atoi(argv[2]);
bool firstTurn = (bool)argv[3];
if(gridSize < 3 || gridSize > 10){
cout << "Incorrect Grid size";
return 0;
}
if(diskAmount < 1 || diskAmount > gridSize){
cout << "Incorrect disk amount";
return 0;
}
NUM_COL = gridSize;
NUM_ROW = gridSize;
WINNING_PUCKS = diskAmount;
FIRST_PLAYER = firstTurn;
}
else{
cout << "Incorrect amount of arguments. Terminating";
return 0;
}
//cout << NUM_COL << endl << WINNING_PUCKS << endl << FIRST_PLAYER << endl;
initBoard(); // initial setup
playGame(); // begin the game
return 0; // exit state
}
It looks to me that you didn't change one of the hard-coded values from your earlier version of the game. On line 336, you have
for (unsigned int r = 3; r < NUM_ROW; r++) {
This is only correct when WINNING_PUCKS is set to 4. The general case should be:
for (unsigned int r = (WINNING_PUCKS - 1); r < NUM_ROW; r++) {
Note that while this part should now work correctly, when I ran it, it still crashes when an end-game is reached with the error message:
double free or corruption (out)
Aborted (core dumped)
I haven't determined what caused this yet.
I'm trying to implement a function that writes double to binary file in little endian byte order.
So far I have class BinaryWriter implementation:
void BinaryWriter::open_file_stream( const String& path )
{
// open output stream
m_fstream.open( path.c_str(), std::ios_base::out | std::ios_base::binary);
m_fstream.imbue(std::locale::classic());
}
void BinaryWriter::write( int v )
{
char data[4];
data[0] = static_cast<char>(v & 0xFF);
data[1] = static_cast<char>((v >> 8) & 0xFF);
data[2] = static_cast<char>((v >> 16) & 0xFF);
data[3] = static_cast<char>((v >> 24) & 0xFF);
m_fstream.write(data, 4);
}
void BinaryWriter::write( double v )
{
// TBD
}
void BinaryWriter::write( int v ) was implemented using Sven answer to What is the correct way to output hex data to a file? post.
Not sure how to implement void BinaryWriter::write( double v ).
I tried naively follow void BinaryWriter::write( int v ) implementation but it didn't work. I guess I don't fully understand the implementation.
Thank you guys
You didn't write this, but I'm assuming the machine you're running on is BIG endian, otherwise writing a double is the same as writing an int, only it's 8 bytes.
const int __one__ = 1;
const bool isCpuLittleEndian = 1 == *(char*)(&__one__); // CPU endianness
const bool isFileLittleEndian = false; // output endianness - you choose :)
void BinaryWriter::write( double v )
{
if (isCpuLittleEndian ^ isFileLittleEndian) {
char data[8], *pDouble = (char*)(double*)(&v);
for (int i = 0; i < 8; ++i) {
data[i] = pDouble[7-i];
}
m_fstream.write(data, 8);
}
else
m_fstream.write((char*)(&v), 8);
}
But don't forget generally int is 4 octects and double is 8 octets.
Other problem is static_cast. See this example :
double d = 6.1;
char c = static_cast(d); //c == 6
Solution reinterpret value with pointer :
double d = 6.1;
char* c = reinterpret_cast<char*>(&d);
After, you can use write( Int_64 *v ), which is a extension from write( Int_t v ).
You can use this method with :
double d = 45612.9874
binary_writer.write64(reinterpret_cast<int_64*>(&d));
Don't forget size_of(double) depend of system.
A little program converting doubles to an IEEE little endian representation.
Besides the test in to_little_endian, it should work on any machine.
include <cmath>
#include <cstdint>
#include <cstring>
#include <iostream>
#include <limits>
#include <sstream>
#include <random>
bool to_little_endian(double value) {
enum { zero_exponent = 0x3ff };
uint8_t sgn = 0; // 1 bit
uint16_t exponent = 0; // 11 bits
uint64_t fraction = 0; // 52 bits
double d = value;
if(std::signbit(d)) {
sgn = 1;
d = -d;
}
if(std::isinf(d)) {
exponent = 0x7ff;
}
else if(std::isnan(d)) {
exponent = 0x7ff;
fraction = 0x8000000000000;
}
else if(d) {
int e;
double f = frexp(d, &e);
// A leading one is implicit.
// Hence one has has a zero fraction and the zero_exponent:
exponent = uint16_t(e + zero_exponent - 1);
unsigned bits = 0;
while(f) {
f *= 2;
fraction <<= 1;
if (1 <= f) {
fraction |= 1;
f -= 1;
}
++bits;
}
fraction = (fraction << (53 - bits)) & ((uint64_t(1) << 52) - 1);
}
// Little endian representation.
uint8_t data[sizeof(double)];
for(unsigned i = 0; i < 6; ++i) {
data[i] = fraction & 0xFF;
fraction >>= 8;
}
data[6] = (exponent << 4) | fraction;
data[7] = (sgn << 7) | (exponent >> 4);
// This test works on a little endian machine, only.
double result = *(double*) &data;
if(result == value || (std::isnan(result) && std::isnan(value))) return true;
else {
struct DoubleLittleEndian {
uint64_t fraction : 52;
uint64_t exp : 11;
uint64_t sgn : 1;
};
DoubleLittleEndian little_endian;
std::memcpy(&little_endian, &data, sizeof(double));
std::cout << std::hex
<< " Result: " << result << '\n'
<< "Fraction: " << little_endian.fraction << '\n'
<< " Exp: " << little_endian.exp << '\n'
<< " Sgn: " << little_endian.sgn << '\n'
<< std::endl;
std::memcpy(&little_endian, &value, sizeof(value));
std::cout << std::hex
<< " Value: " << value << '\n'
<< "Fraction: " << little_endian.fraction << '\n'
<< " Exp: " << little_endian.exp << '\n'
<< " Sgn: " << little_endian.sgn
<< std::endl;
return false;
}
}
int main()
{
to_little_endian(+1.0);
to_little_endian(+0.0);
to_little_endian(-0.0);
to_little_endian(+std::numeric_limits<double>::infinity());
to_little_endian(-std::numeric_limits<double>::infinity());
to_little_endian(std::numeric_limits<double>::quiet_NaN());
std::uniform_real_distribution<double> distribute(-100, +100);
std::default_random_engine random;
for (unsigned loop = 0; loop < 10000; ++loop) {
double value = distribute(random);
to_little_endian(value);
}
return 0;
}
I am making an IRC bot with C++ and the Qt Library for C++. I have looked around, but could not find an algorithm I liked for binary to decimal conversion. My decimal to binary does not work at all, but my bin to dec returns an answer, shifted to the lift 2 bits. Here is my code:
QString MainWindow::toBin(bool isneg, QString num) {
QString buffer;
bool boolBuffer;
int intBuffer;
int bitNum;
if(isneg) {
boolBuffer = true;
intBuffer = 0;
QString out;
while(boolBuffer) {
if(pow(2, (double)intBuffer) >= num.toDouble()) {
boolBuffer = false;
} else {
intBuffer++;
}
}
bitNum = intBuffer;
intBuffer = num.toInt();
buffer = "1";
intBuffer = intBuffer - pow(2, bitNum);
bitNum--;
for(int i=bitNum;i>=0;i--) {
if(intBuffer - pow(2, i) >= 0) {
intBuffer = intBuffer - pow(2, i);
buffer.append("1");
} else {
buffer.append("0");
}
}
for(int i=0;i<buffer.length();i++) {
if(buffer.at(i)=='1') out.append("0");
else out.append("1");
}
out.append(" + 1");
return out;
} else {
boolBuffer = true;
intBuffer = 0;
while(boolBuffer) {
if(pow(2, (double)intBuffer) >= num.toDouble()) {
boolBuffer = false;
} else {
intBuffer++;
}
}
bitNum = intBuffer;
intBuffer = num.toInt();
buffer = "1";
intBuffer = intBuffer - pow(2, bitNum);
bitNum--;
for(int i=bitNum;i>=0;i--) {
if(intBuffer - pow(2, i) >= 0) {
intBuffer = intBuffer - pow(2, i);
buffer.append("1");
} else {
buffer.append("0");
}
}
return buffer;
}
}
QString MainWindow::toDec(bool isneg, QString num) {
QString buffer;
int intBuffer;
if(isneg) {
buffer = num;
intBuffer = 0;
for(int i=0;i<buffer.length();i++) {
if(buffer.at(i)=='1') intBuffer = intBuffer + pow(2, abs(buffer.length()-i-1));
}
buffer = QString::number(-1*intBuffer);
return buffer;
} else {
buffer = num;
intBuffer = 0;
for(int i=0;i<buffer.length();i++) {
if(buffer.at(i)=='1') intBuffer = intBuffer + pow(2, abs(buffer.length()-i-1));
}
buffer = QString::number(intBuffer);
ui->textBrowser->append(buffer);
return buffer;
}
}
Everything else works fine, I have tested it and it is called, the sending of the message is fine, and I have gone through and manually inputed the QString "10", then ran through the code in my head while writing down vars in a document, and I came to the conclusion 2, but when I call the function it returns 8.
You can approach this differently. Instead of trying to compute the bit value at each string position, just double your number every time you get a new digit. This is what is done by most algorithms out there (you know, the ones you didn't like):
QString MainWindow::toDec(bool isneg, QString num) {
int val = 0;
for( int i = 0; i < num.length(); i++ ) {
val = (val << 1) + num.at(i).digitValue();
}
return QString::number( isneg ? -val: val);
}
I'm not sure about your "negative" representation though. Binary negatives are usually in two's-complement form. This decimal conversion will not work for that.
[edit] You know, I just had a thought that your input is not sensible. Both your algorithm and my simplification rely on the string containing only 0 or 1 characters. But I'd be willing to bet you have other characters in that string.
The fix for this would demand a modification of the loop:
for( int i = 0; i < num.length(); i++ )
{
int bit = num.at(i).digitValue();
if( bit != 0 && bit != 1 ) break;
val = (val << 1) + bit;
}
I wonder how to reverse something like this. So having a mask where auto mask = 1ULL << 20; how to get 20 out from mask?
Loop-free
Many years ago when I was writing a bit-wise arithmetic for a chess engine, I found a fast implementation which is useful for your requirement, it's loop-free. This method will return the position of the first 1-bit from right-to-left (Least Significant Bit):
inline unsigned int lsb(unsigned long long value)
{
if (!value)
return -1;
value &= -value;
unsigned int lsb = (unsigned) value | (unsigned) (value >> 32);
return (((((((((((unsigned) (value >> 32) != 0) << 1)
+ ((lsb & 0xffff0000) != 0)) << 1)
+ ((lsb & 0xff00ff00) != 0)) << 1)
+ ((lsb & 0xf0f0f0f0) != 0)) << 1)
+ ((lsb & 0xcccccccc) != 0)) << 1)
+ ((lsb & 0xaaaaaaaa) != 0);
}
int main()
{
unsigned long long x = 1ULL<<20;
cout << lsb(x) << endl;
}
Output
20
I think, I had found it here.
Using log:
#include <iostream>
#include <cmath>
int main() {
auto mask = 1ULL << 20;
std::cout << log2(mask) << std::endl;
// edit out: std::cout << log(mask) / log(2) << std::endl;
return 0;
}
or loop and shift:
#include <iostream>
int main() {
auto mask = 1ULL << 20;
for (unsigned int c = 0; c < sizeof(mask) * 8 && mask; c++) {
mask >>= 1;
if (mask == 0)
std::cout << c << std::endl;
}
return 0;
}
If it's a 64-bit mask, you can compute it modulo 67 and do a table lookup.
To wit:
static int table[67] = {
-1, 0, 1,39, 2,15,40,23, 3,12,
16,59,41,19,24,54, 4,-1,13,10,
17,62,60,28,42,30,20,51,25,44,
55,47, 5,32,-1,38,14,22,11,58,
18,53,63, 9,61,27,29,50,43,46,
31,37,21,57,52, 8,26,49,45,36,
56, 7,48,35, 6,34,33};
int unmask(unsigned long long ull) {
return table[ull % 67];
}
//first if you want to make sure only 1 bit is "on" you can do that:
if ((mask & mask-1) != 0)
{
//you have more than 1 bit "on", deal with it...
}
//finding which bit is "on" can be achieve in a loop
int count 0;
while (mask > 1)
{
mask>>=1;
count++;
}
//At this point count will have the required value (20 in your example)
Option 1: iterate
while (mask && !(mask & 1)) { mask>>=1; count++; }
Option 2: iterate multiple bits at a time:
unsigned long long a=0xFFFFFFFFULL; int b=32;
while (mask>1) {
if (!(mask & a)) { count+=b; mask>>=b; }
b>>=1; mask>>=b;
}
Option 3: Convert the mask to double or float and extract the exponent.
union {
struct {
int mantissa:23;
int exp:7;
int sign:1;
} s;
float f;
} u = { (float) mask };
return u.s.exp + 1;
A simple loop will be quite okay:
for (int bit = 0; bit < sizeof(mask) * 8; bit++)
{
if ((1ULL << bit) & mask)
std::cout << "Bit " << bit << " is set in the mask\n";
}
How about a TMP solution:
#include <iostream>
template < unsigned long long MASK >
struct MaskIndex
{
enum { i = MaskIndex < MASK / 2 >::i + 1 };
};
template <>
struct MaskIndex < 1 >
{
enum { i = 0 };
};
int main()
{
const unsigned long long mask = 1ULL << 20;
std::cout << MaskIndex < mask >::i << std::endl;
return ( 0 );
}
You can try this..
if((1ULL<<20)&mask) {
cout << "20th bit is set";
}
Ok, I've been trying to self learn C++ and as such decided to try make an encrypt/decrypt program. The idea is to open a file and edit bits according to the password. I'm having some problems with my code and by using break-points I have found that the error arises when I open the file (it is in the main() about a third of the way down). Visual C++ tells me that the heap has become corrupt, and I'm at a loss as to why. Any help would be greatly appreciated.
#include <iostream>
#include <fstream>
#include <sstream>
#include <stdio.h>
#include <fstream>
#include <sys/stat.h>
using namespace std;
unsigned char fileData[31];
bool *password;
int count(0), maxCount;
/*
* Programmer: P7r0
* Program: Encrypt/Decrypt
* Version: InDev
* Date Released: -
*
* Notes:
* -
*/
struct bits{
// Breaks each byte into its 8 bits
unsigned int b1 : 1;
unsigned int b2 : 1;
unsigned int b3 : 1;
unsigned int b4 : 1;
unsigned int b5 : 1;
unsigned int b6 : 1;
unsigned int b7 : 1;
unsigned int b8 : 1;
} ;
// Toggles the bits, ie if 1 make 0
int swap(int Obj){
if (Obj = 1){return 0;}
else if (Obj = 0){return 1;}
}
void conversion(string convert){
// User password to a boolean array
int ascii, loop, count, a, counter(0);
const char *code;
bool bin [ ] = {false,false,false,false,false,false,false,false};
// Create an array for the booleans
password = new bool [convert.length()];
code = convert.c_str();
for (loop = 0;loop < convert.length(); loop++){
for (a = 0;a < 8;a++){bin[a] = false;}
// Get the equivilent ASCII code
ascii = int(code[loop]);
while (ascii > 0){
// Develop a tempory binary array with code based off of the ASCII values
if (ascii >= 128){ascii -= 128;bin[0] = true;}
else if (ascii >= 64){ascii -= 64;bin[1] = true;}
else if (ascii >= 32){ascii -= 32;bin[2] = true;}
else if (ascii >= 16){ascii -= 16;bin[3] = true;}
else if (ascii >= 8){ascii -= 8;bin[4] = true;}
else if (ascii >= 4){ascii -= 4;bin[5] = true;}
else if (ascii >= 2){ascii -= 2;bin[6] = true;}
else if (ascii >= 1){ascii -= 1;bin[7] = true;}
}
for (count = 0; count < 8; count++){
// Move out of the tempory array into the main array for global use
//cout << bin[count];
password[counter] = bin[count];
counter++;
}
//cout << ":\n";
}
}
int encrypt(int loop){
// Changes everything bit by bit in blocks of bytes the size of loop, typically 32
int a, b, counter(0);
bits bit;
for (a = 0; a == loop; a++){
bit = * (bits*)(&fileData[a]);
cout << bit.b1 << "\t";
for (b = 0; b == 7; b++){
if (count = maxCount){count = 0;}
if (password[count] = true){
// If current password array is true then toggle current bit
if (b = 0){bit.b1 = swap(bit.b1);}
else if (b = 1){bit.b2 = swap(bit.b2);}
else if (b = 2){bit.b3 = swap(bit.b3);}
else if (b = 3){bit.b4 = swap(bit.b4);}
else if (b = 4){bit.b5 = swap(bit.b5);}
else if (b = 5){bit.b6 = swap(bit.b6);}
else if (b = 6){bit.b7 = swap(bit.b7);}
else if (b = 7){bit.b8 = swap(bit.b8);}
count++;}
else {count++;}
}
cout << counter;
fileData[counter] = *(unsigned char*)(&bit);
counter++;
}
return 0;
}
int main(){
fstream file;
char *remainder;
int counter, size, temp, b(0), stackCount(0);
long begin, end;
string usrin, pass, pause, filedir;
cout << "Please input password, must be one word\n";
cin >> pass;
maxCount = pass.length();
conversion(pass);
// Change password data stored at its location as to avoid unwanted detection of the password
pass = "default";
cout << "\nPlease input file path\n";
cin >> filedir;
//The error seems to be here
file.open(filedir.c_str(),ios::in | ios::out | ios::binary);
// Check that the file is open
if (file.is_open()){
cout << "Encrypting...\n";
counter = 32;
// Work out size (bytes) of the file
begin = file.tellg();
file.seekg(0,ios::end);
end = file.tellg();
file.seekg(0,ios::beg);
b = file.tellg();
size = end-begin;
while((int)b <= size){
// Had to typecast as the unsigned/signed mis-match was throwing compile errors
file.read((char*)(&fileData),counter);
encrypt(counter);
if (size - b >= 32){
file.write((char*)(&fileData),counter);
b = file.tellg();
} else if (size - b < 32 && size - b > 0) {
remainder = new char [size - b];
for (int a = 0; a != size - b; a++){remainder[a] = fileData[a];}
file.write((char*)(remainder),size - b);
// To cancel out of the while loop
b += 1;
} else if (size - b == 0){b += 1;}
}
file.close();
cout << "\nEncrypted.\nPlease enter a letter to continue\n";
cin >> pause;
// Prompt user if unable to open the file
} else {cout << "Failed to open the file";}
return 0;
}
In your conversion()-method you have the following code:
int counter(0);
// ...
password = new bool [convert.length()];
// ...
for (count = 0; count < 8; count++){
password[counter] = bin[count];
counter++;
}
If the length of convert is less than 8, you will be writing outside the password-array inside the loop.
A heap-corruption will usually not be detected at once, which is why you do not get the error until opening the file.
Not sure if this is the cause of your problem, but in any case it is unwise to write directly to the file that you are reading from. Write to a temp file and rename the files when done.