LinuxQuestions.org
Did you know LQ has a Linux Hardware Compatibility List?
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 12-06-2012, 04:49 PM   #1
R2B Boondocks
LQ Newbie
 
Registered: Nov 2012
Posts: 16

Rep: Reputation: Disabled
Updating a grid


Hi all,

I am curious as to how I would get my grid to be updated from a user's choice. I have gotten this much code so far in my .cpp file
Code:
#include <iostream>
#include <string>
#include <sstream>
#include "game.h"
#include "grid.h"
#include "user.h"


using namespace std;


//A non-alphabetic character
const char Grid::FILL_CHARACTER = '.';

//Set up a new Game
char Grid::printGrid (char grid [][7], int, int)
{
    const int rows = 7;
    const int columns = 7;
    char g [rows][columns] =
    {
        {'.', '.', '.', '.', '.','.','.'},
        {'.', '.', '.', '.', '.','.','.'},
        {'.', '.', '.', '.', '.','.','.'},
        {'.', '.', '.', '.', '.','.','.'},
        {'.', '.', '.', '.', '.','.','.'},
        {'.', '.', '.', '.', '.','.','.'},
        {'1', '2', '3', '4', '5','6','7'},
    };
    for (int i=0; i < rows; i++)
        {
            for (int j = 0; j < columns; j++)
            {
                cout << grid [i][j] << endl;
            }
        }
    cout << "Letters remaining to you" << endl;
    cout << User::hLetters();
}

// Gather human player's moves
char User::hMoves (char moves)
{
    {
    string input = "";

char myChar ={0};

while (true) {
    cout << "What letter would you like to drop?";
    getline (cin, input);

    if (input.length() == 1) {
        myChar = input [0];
        break;
                                }
    cout << "Please enter only a single character." << endl;
    if (isalpha (myChar)) {
        if (isupper (myChar)) {
        myChar =tolower (myChar);
                                }
                            }
            }
    }

    string input ="";

    int myInt = 0;
    while (true) {
        cout << "What column would you like to drop that in? (1-7)";
        getline (cin, input);

        stringstream myStream(input);
        if (myStream >> myInt)
            break;
        cout << "What column would you like to drop that in? (1-7)";
                }
}

//Update the grid reflecting player's choice
char Grid::updateGrid (char grid [][7], int, int)
{

}
Here are the .h files that I included.
Code:
#ifndef GRID_H
#define GRID_H

class Grid {

public:

  //A non-alphabetic character
  static const char FILL_CHARACTER;

  //Initialize the grid for a game using
  //set up from indicated file
  char printGrid (char grid [][7], int, int);


  //After a player's move, computer or user, update the grid
  //reflecting their choices
  char updateGrid (char grid [][7], int, int);

  //If the grid is completely filled annouce game over followed
  //by whoever one
  void fullGrid ();

};


#endif
Code:
#ifndef USER_H
#define USER_H


class User {
public:

  //Print the letters remaining to the user
  static const char *hLetters ()
{
    return "aabcdeefghiijklmnoopqrstuuvwxyz";
}
  //Gather user's moves
  char hMoves (char moves);

  //Update the grid, reflecting the player's choice
  void updateGrid ();

  //Determine word(s) user wishes to claim
  void wordCheck (const char* wordListFileName);

  //Following a claim, determine point value, if any, and print
  //the turn score
  void awardPoints (int wordLength);


};


#endif
Code:
#ifndef GAME_H_INCLUDED
#define GAME_H_INCLUDED

#include <string>

class Game {
public:

//A non-alphabetic character
static const char FILL_CHARACTER;

//Set up a new game
Game (char grid);

//Indicates whether the game is finished and, if so,
// if the human or computer has won
bool humanHasWon();

//Indicates wheter the game is finished and, if so,
// if the human or computer has lost
bool humanHasLost();

//Update game state to reflect a new move
void moveHasBeenMade (char move);


};


#endif // GAME_H_INCLUDED
Thanks for any help.
 
Old 12-08-2012, 11:22 AM   #2
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian
Posts: 2,514

Rep: Reputation: 855Reputation: 855Reputation: 855Reputation: 855Reputation: 855Reputation: 855Reputation: 855
Quote:
I have gotten this much code so far in my .cpp file
Am I correct in assuming you haven't written your main() function yet? Meaning you've written a whole bunch of code that has never been tested? A better approach is to build up the program in steps, where each step is a correctly working program.

Quote:
I am curious as to how I would get my grid to be updated from a user's choice.
Since you don't appear to be storing a grid anywhere in your program, it might be hard to update it...
 
Old 12-08-2012, 11:53 AM   #3
markush
Senior Member
 
Registered: Apr 2007
Location: Germany
Distribution: Slackware
Posts: 3,971

Rep: Reputation: 849Reputation: 849Reputation: 849Reputation: 849Reputation: 849Reputation: 849Reputation: 849
Quote:
Originally Posted by ntubski View Post
... A better approach is to build up the program in steps, where each step is a correctly working program.
...
Wouldn't that be Lisp, where one writes functions in the REPL and can test them independently?

Markus

Last edited by markush; 12-08-2012 at 11:55 AM.
 
Old 12-08-2012, 04:34 PM   #4
R2B Boondocks
LQ Newbie
 
Registered: Nov 2012
Posts: 16

Original Poster
Rep: Reputation: Disabled
Lol ya I never did do a main. Sadly it is too late now, I have already submitted my assignment as is
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
grid storage td3201 Linux - Software 0 03-04-2011 06:15 PM
Grid Computing. azfar Other *NIX 3 01-17-2009 03:51 AM
grid control 10.1.0.3 waqar General 0 03-27-2006 04:09 AM
Grid Computing Oxagast Linux - General 4 12-01-2005 03:30 AM
The Grid And Linux JerryMcFarts General 7 02-24-2005 10:58 AM


All times are GMT -5. The time now is 06:54 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration