Thursday 12 April 2012

A work in progress: Accuracy game [Delphi]

I'm currently working on a Delphi game. The rules are as following: On the right there is a model with a color that changes. Inside the game field are more circles which randomly appear and change colors and your task is to click the correct color and accurately. It's still a work in progress, but I can post some pictures already. Sorry for not including the code, it's rather long (I suppose I could send it to you if you wanted). As it's a work in progress, many things don't work yet (visible on the screenshots). I'll be finishing this soon though.



Sunday 8 April 2012

A very simple Blackjack game [C++]

Here's another of my creations, this time a very, very simple Blackjack game that doesn't even have an opponent to play against. But it took me quite a while. C++ sure is a hard language. Here's the code and of course an image to demonstrate what it does.

#include 
#include 

using namespace std;

// Global Variables //

char *card_num[] = {"2","3","4","5","6","7","8","9","10","Jack","Queen","King","Ace"};
char *card_type[] = {"Clubs","Diamonds","Spades","Hearts"};


// Structures //

struct cardtype {
    int color;
    int number;
    int value;
};

struct decktype {
    bool deck[52];
    int size;
};

// Functions //

void SeedRandom() {
    time_t tTime;
    time(&tTime);
    srand(tTime);
    rand();
}

struct cardtype PickACard(struct decktype &deck) {
    struct cardtype card;
    int randnum = (int) (1.0 * rand() / (RAND_MAX + 1) * deck.size);
    int counter = 0;
    int i;
    for (i=0; i<52; ++i) {
        if (deck.deck[i] == true) {
            ++counter;
        }
        if (counter == randnum+1) {
            break;
        }
    }
    --deck.size;
    deck.deck[i] = false;
    card.number = i / 4;
    card.color = i % 4;
    if (card.number == 10 || card.number == 11) {
        card.value = 10;
    } else if (card.number == 9) {
        card.value = 1;
    } else if (card.number == 12) {
        card.value = 11;
    } else {
        card.value = card.number + 2;
    }
    return card;
}

struct decktype DeckInit() {
    struct decktype deck;
    deck.size = 52;
    for (int i=0; i<52; ++i) {
        deck.deck[i] = true;
    }
    return deck;
}

char *TextCard(struct cardtype &card) {
    char *str = (char *) malloc (22 * sizeof(char));
    sprintf(str,"%s of %s (%d)",card_num[card.number],card_type[card.color],card.value);
    return str;
}

// Main //

void main() {
    SeedRandom();

    bool IsGameOver = false;
    char WantCard;

    int totalvalue = 0;

    cout << "Welcome to blackjack!" << endl;
    struct decktype deck = DeckInit();

    while (!IsGameOver) {
        bool ValidChoice = false;
        while (!ValidChoice) {
            cout << "Do you want a card? (Y)es / (N)o" << endl;
            cin >> WantCard;
            if (WantCard == 'Y') {
                struct cardtype card = PickACard(deck);
                totalvalue += card.value;
                cout << "You received a " << TextCard(card) << ", current total value at " << totalvalue << "." << endl;
                if (totalvalue > 21) {
                    cout << "Sorry, you have exceeded 21 points and thus lost!" << endl;
                    IsGameOver = true;
                }
                if (totalvalue == 21) {
                    cout << "Congratulations, you have won!" << endl;
                    IsGameOver = true;
                }
                ValidChoice = true;
            } else if (WantCard == 'N') {
                cout << "You stopped prematurely, and since there isn't an opponent, you won :)" << endl;
                ValidChoice = true;
                IsGameOver = true;
            } else {
                cout << "Invalid choice, please try again";
            }
        }

    }
    cout << "The game is over now, thanks for playing!" << endl;

 cin.ignore();
 cin.get();
}


Thursday 5 April 2012

My go at TicTacToe [C++]

Today I started getting into C++ with my previous experiences with Pascal. I tried writing a simple Tictactoe program in the console. There were some difficulties, but overall I'm quite happy with how it worked out :D. It's probably not very effective and the conditions whether a player won or not are pretty clustered, but for a first try I'm content.

Here's the code.

#include 
using namespace std;

void main() {
 // Declarations
 char rgBoard[9] = {'1','2','3','4','5','6','7','8','9'};
 bool bIsGameOver = false;
 int iPlayer = 1;
 
 int iTurn;
 bool bTurn = false;

 // Print initial board
 cout << rgBoard[0] << "|" << rgBoard[1] << "|" << rgBoard[2] << endl;
 cout << "-----" << endl;
 cout << rgBoard[3] << "|" << rgBoard[4] << "|" << rgBoard[5] << endl;
 cout << "-----" << endl;
 cout << rgBoard[6] << "|" << rgBoard[7] << "|" << rgBoard[8] << endl << endl;

 // Game loop
 while (bIsGameOver == false) {
  // Assignments
  bTurn = false;

  // Play a turn
  cout << "Player " << iPlayer << ", it's your turn." << endl;
  while (bTurn == false) {
   cin >> iTurn;
   if (iTurn >= 1 && iTurn <= 9 && rgBoard[iTurn-1] != 'X' && rgBoard[iTurn-1] != 'O') {
    bTurn = true;
    cout << endl;
   } else {
    cout << "Invalid move, please try again." << endl;
   }
  }

  if (iPlayer == 1) {
   rgBoard[iTurn-1] = 'X';
   iPlayer = 2;
  } else if (iPlayer == 2) {
   rgBoard[iTurn-1] = 'O';
   iPlayer = 1;
  }

  // Check if the game is over
  if ((rgBoard[0] == 'X' && rgBoard[1] == 'X' && rgBoard[2] == 'X') ||
   (rgBoard[0] == 'X' && rgBoard[3] == 'X' && rgBoard[6] == 'X') ||
   (rgBoard[0] == 'X' && rgBoard[4] == 'X' && rgBoard[8] == 'X') ||
   (rgBoard[1] == 'X' && rgBoard[4] == 'X' && rgBoard[7] == 'X') ||
   (rgBoard[2] == 'X' && rgBoard[5] == 'X' && rgBoard[8] == 'X') ||
   (rgBoard[3] == 'X' && rgBoard[4] == 'X' && rgBoard[5] == 'X') ||
   (rgBoard[6] == 'X' && rgBoard[7] == 'X' && rgBoard[8] == 'X') ||
   (rgBoard[2] == 'X' && rgBoard[4] == 'X' && rgBoard[6] == 'X')) {
    cout << "Player 1 wins!" << endl << endl;
    bIsGameOver = true;
  } else if ((rgBoard[0] == 'O' && rgBoard[1] == 'O' && rgBoard[2] == 'O') ||
   (rgBoard[0] == 'O' && rgBoard[3] == 'O' && rgBoard[6] == 'O') ||
   (rgBoard[0] == 'O' && rgBoard[4] == 'O' && rgBoard[8] == 'O') ||
   (rgBoard[1] == 'O' && rgBoard[4] == 'O' && rgBoard[7] == 'O') ||
   (rgBoard[2] == 'O' && rgBoard[5] == 'O' && rgBoard[8] == 'O') ||
   (rgBoard[3] == 'O' && rgBoard[4] == 'O' && rgBoard[5] == 'O') ||
   (rgBoard[6] == 'O' && rgBoard[7] == 'O' && rgBoard[8] == 'O') ||
   (rgBoard[2] == 'O' && rgBoard[4] == 'O' && rgBoard[6] == 'O')) {
    cout << "Player 2 wins!" << endl << endl;
    bIsGameOver = true;
  } else if (rgBoard[0] != '1' && rgBoard[1] != '2' && rgBoard[2] != '3' && rgBoard[3] != '4' &&
   rgBoard[4] != '5' && rgBoard[5] != '6' && rgBoard[6] != '7' && rgBoard[7] != '8' && rgBoard[8] != '9') {
   cout << "It's a tie!" << endl << endl;
   bIsGameOver = true;
  }

  // Print board
  cout << rgBoard[0] << "|" << rgBoard[1] << "|" << rgBoard[2] << endl;
  cout << "-----" << endl;
  cout << rgBoard[3] << "|" << rgBoard[4] << "|" << rgBoard[5] << endl;
  cout << "-----" << endl;
  cout << rgBoard[6] << "|" << rgBoard[7] << "|" << rgBoard[8] << endl << endl;
 }

 cin.ignore();
 cin.get();
}

And this is how it looks when ran.


Follow us