ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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
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...
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.