trying to simulate a soocer game shootout using arrays - c++

I have to create a game of 5 rounds simulating a soccer shootout using a 2x3 array that represents the goal. The computer randomly picks 3 places to block and the user chooses one place to shoot. If the user chooses a coordinate that is not blocked then its a goal. Two functions are needed, one where the computer picks 3 random places to block and the other function is prints out the goal every round. If the user scores 3 times then they win, otherwise they lose.
The output should look like this(B=Blocked, G=Goal, "-" = empty space):
B - B
B - G
Ive been stuck on my code and have gotten an error that I just cant seem to fix within both functions
#include <iostream>
#include <stdlib.h>
#include <cmath>
#include <ctime>
using namespace std;
void computerPick(char soccer[]);
void shot(char shooter[]);
int main()
{
int userInputX;
int userInputY;
srand(time(NULL));
char soccer[2][3];
for(int i=0; i<2; i++)
{
for(int j=0; j<3; j++)
{
soccer[i][j]='-';
}
}
cout<<"Pick a X coordinate to shoot at: "<<endl;
cin>>userInputX;
cout<<"Pick a Y coordinate to shoot at: "<<endl;
cin>>userInputY;
computerPick(soccer);
shot(soccer,userInputY,userInputX);
}
void computerPick(char soccer[])
{
int x = rand()%3;
int y = rand()%2;
soccer[x][y]='B';
}
void shot(char shooter[], int userInputY, int userInputX)
{
int score=0;
if(shooter[userInputX][userInputY]!='B')
cout<<"shot is good"<<endl;
else
cout<<"shot is blocked"<<endl;
}

You have to use correct types for arguments and have to match the prototype declaration and definition of functions.
This code compiles:
#include <iostream>
#include <stdlib.h>
#include <cmath>
#include <ctime>
using namespace std;
void computerPick(char soccer[][3]);
void shot(char shooter[][3], int userInputY, int userInputX);
int main()
{
int userInputX;
int userInputY;
srand(time(NULL));
char soccer[2][3];
for(int i=0; i<2; i++)
{
for(int j=0; j<3; j++)
{
soccer[i][j]='-';
}
}
cout<<"Pick a X coordinate to shoot at: "<<endl;
cin>>userInputX;
cout<<"Pick a Y coordinate to shoot at: "<<endl;
cin>>userInputY;
computerPick(soccer);
shot(soccer,userInputY,userInputX);
}
void computerPick(char soccer[][3])
{
int x = rand()%3;
int y = rand()%2;
soccer[x][y]='B';
}
void shot(char shooter[][3], int userInputY, int userInputX)
{
int score=0;
if(shooter[userInputX][userInputY]!='B')
cout<<"shot is good"<<endl;
else
cout<<"shot is blocked"<<endl;
}

You might want to look at these parts again:
char soccer[2][3];
and
int x = rand()%3;
int y = rand()%2;
soccer[x][y]='B';
Also note that booleans would be clearer in your two dimensional array instead of chars of 'B' or 'G'.
Also when using multidimensional arrays as parameters, you can pass them as
int foo(int (*array)[5][10])
Which means that you are passing a pointer to an array of fixed size 5-10

Related

Vector of multiple classes do not shuffle as expected using random_shuffle [duplicate]

This question already has answers here:
How to make sure that std::random_shuffle always produces a different result?
(6 answers)
Closed 4 years ago.
Decided to create a deck in C++ using a vector and a Card class. I chose this because shuffling is supposed to be easy using random_shuffle. My first approach just to get aquatint with C++ I made a Card list, used vector to generate numbers and shuffle them just to insert the cards in a tmp list and return it. This was messy to say the least.
So on my second branch I tried to do it with a vector from the start, it looks like this:
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
// Represent a card
class Card
{
private:
int suit;
int value;
public:
Card(){}
Card(int v, int s)
{
suit = s;
value = v;
}
void
setNewVals(int v, int s)
{
value = v;
suit = s;
}
int
getValue()
{
return value;
}
int
getSuit()
{
return suit;
}
};
// Shuffle deck
void
shuffleDeck(vector<Card> *c, int shuffles)
{
for(int s = 0; s < shuffles; s++)
{
random_shuffle(c->begin(), c->end());
}
}
int
main()
{
//TODO: Later input from main
int numDecks = 10;
int numShuffles = 10;
// Create a deck vector
vector<Card> c;
// Select card's value
for(int d = 0; d < numDecks; d++)
{
for(int v = 1; v < 14; v++)
{
// Select card's suit
for(int s = 0; s < 4; s++)
{
Card card(v, s);
c.push_back(card);
}
}
}
// Shuffle the deck
shuffleDeck(&c, numShuffles);
// Print out first 52 cards
for(int i = 0; i < 52; i++)
{
Card aCard = c.front();
cout << aCard.getValue() << "\t" << aCard.getSuit() << endl;
c.erase(c.begin());
}
return 0;
}
The problem is that now the random_shuffle does not really work as expected. It shuffles alright, but all cards always end up in the same order. When I did almost the exact same thing with int, it worked fine.
I really have no idea why it is not shuffling random all the time. Have I misinterpreted how vectors work?
You should see http://www.cplusplus.com/reference/algorithm/random_shuffle/ link.
The randomness of std::random_shuffle() seems depends on the std::srand function.
so, you should code like follows (It is just one example)
#include <iostream>
#include <algorithm>
#include <vector>
#include <ctime>
...
int main() {
...
// Make randomness for shuffle
std::srand(unsigned(std::time(0)));
// Shuffle the deck
shuffleDeck(&c, numShuffles);
...
}
In order for std::random_shuffle to return different values at different runs, it needs to be given a non-fixed seed.
You can solve this by adding the line:
std::srand(std::time(NULL));
Now executions at least 1 second apart will give different shuffling results.

C++ basic coding issue

#include <iostream>
using namespace std;
int main() {
int a=0,b=0;
cin>>a>>b>>endl;
for(int i=a;i<=b;++i)
cout<<i<<endl;
return 0;
}
I want to see the output is about the integers inclusive between a and b, but after entering two numbers, it shows no output..
#include <iostream>
using namespace std;
int main() {
int a=0,b=0;
cin>>a>>b>>endl;
for(int i=a;i<=b;++i)
cout<<i<<endl;
return 0;
}
First you can't use endl in cin
Second you wrote ++i inside your for loop which will increase value of by i means value of will become 1 from 0.
Therefore the condition will never be true as value of b is 0.
THE CORRECT WAY
#include <iostream>
using namespace std;
int main() {
int a=0,b=0;
cin>>a>>b;
for(int i=a;i<=b;i++)
cout<<i<<endl;
return 0;
}
#include <iostream>
using namespace std;
int main() {
int a = 0, b = 0;
cin >> a;
cin >> b;
for (int i = a; i <= b; i++)
cout << i << endl;
return 0;
}
EDIT: I removed something as it wasn't true :P Silly me.
Also 'endl' doesn't work with cin :)
The code is wrong because you have already got a and b equal to 0,and after that you are taking a and b as inputs.
If you wanna take them as inputs you should write int a,b. NOT int a=0,b=0

c++ Scrabble game using arrays

I have to make a scrabble scoring game using 2 arrays. The first array holds the user inputted word and the second array holds the value of each letter. Lastly a function is needed to calculate the score. Im having trouble assigning the user values to the second array to get the score and getting the right code for the function.
#include <iostream>
#include <stdlib.h>
#include <cmath>
#include <ctime>
using namespace std;
int scoreCalculator(int total);
char userWord;
int points;
int total=0;
int main()
{
char userWord[11];
for (int i=0; i<11; i++)
{
userWord[i]='\0';
}
cout<<"Enter your word less than 10 letters: "<<endl;
cin>>userWord;
cout<<"Here is the word you inputted: "<<userWord<<endl;
int scrabblePoints[26]={1,3,3,2,1,4,2,4,1,8,5,1,3,1,1,3,10,1,1,1,1,4,4,8,4,10};
for(int j=0; j<26; j++)
{
userWord[i]
}
cout<<"Here is your score: "<<scoreCalculator(total)<<endl;
}
int scoreCalculator(int total)
{
total+=scrabblePoints[j];
}
This is what I have so far and where im stuck at
#include <iostream>
int main()
{
std::string input;
std::cin>>input;
// fill this with scrable values
int scrable_values[26] = {1,3,3,2,1,4,2,4,1,9,5,1,3,1,1,3,10,1,1,1,1,4,4,8,4,10};
int ans = 0;
for(int i=0;i<input.size();i++)
{
ans += scrable_values[input[i]-97];
}
return ans;
}

Inheritance Robot Race that doesn't update array

I am working on a program that is suppose to have 3 different Robots racing on a track. The track is suppose to be 100 in length. I have just learned inheritance and still trying to understand how to connect data members from one .h to another. When I run my program, nothing happens when I call any of my Robots. I will show one of them as an example. Can you explain how to make their movements update the race 2D array?
robotRace.h
#ifndef ROBOTRACE_H
#define ROBOTRACE_H
using namespace std;
class robotRace {
public:
robotRace (); //constructor
static const int rows = 5;
static const int columns = 100;
protected:
int race[rows][columns]; //initial base for race floor
};// end superclass robotRace that should do no movement
#endif
robotRace.cpp
#include <iostream>
#include "robotRace.h"
using namespace std;
robotRace :: robotRace() {
for (int i = 0; i < rows; i++)
for (int j= 0; j<columns; j++)
race[i][j] = ' ';
}//end constructor
This is one of the Robots and their functions to update array. Not sure how to make it work.
FunctionRobot.h
#ifndef FUNCTIONROBOT_H
#define FUNCTIONROBOT_H
#include "robotRace.h"
using namespace std;
class FunctionRobot : public robotRace{
public:
FunctionRobot();
int position(int);
void print();
protected:
};
#endif
FunctionRobot.cpp
#include <iostream>
#include "FunctionRobot.h"
#include <cmath>
using namespace std;
FunctionRobot :: FunctionRobot (): robotRace() {
int initPos =0;
race[initPos][0] = '*';
cout <<"Initial position of Function Robot is at begin of race."<<endl;
}
int FunctionRobot :: position(int place=0){
// log with a base 2 needs to be divided by the "x"
// below is the Robots formula to determine each of their movements
double x = ( 2 * (log(place)/log(2)));
return (int) x;
}
void FunctionRobot :: print(){
for (int i;i=0; i<100; i++)
for (int j;j=0; j<1; j++)
race[position()][j];
}
this is my main file as requested. This is basic format. I am hoping to make the while loop more practical so that the user doesn't have to keep entering 1.
There is also no error coming from my code. It runs just shows nothing.
main.cpp
#include <iostream>
#include "robotRace.h"
#include "FunctionRobot.h"
using namespace std;
int main() {
int userInput;
cout << "Welcome to the Robot Race of the year!" << endl;
cout << "For our contestants we have the amazing three!" << endl;
cout << "The contestants are Robots F, R and U" << endl;
cout << "Let the games begin! \n\n";
cout << "Enter 1 to begin. " << endl;
cin >> userInput;
FunctionRobot functionObj;
//functionObj.position();
//functionObj.print();
cout << "Ready... Set... Go!!" << endl;
while (userInput == 1) {
functionObj.position(4);
functionObj.print();
} //end while
return 0;
}
Your print() goes out of bounds:
void FunctionRobot :: print(){
for (int i; i<100; i++)
for (int j; j<1; j++)
race[position()][j];
}
j is not initialized. You could try int j = 0 for a start. Similar for i.
Moreover you know that this function is named PRINT but doesn't PRINT ANYTHING, actually it doesn't do anything but calling position().
int FunctionRobot :: position(int place=0){
// log with a base 2 needs to be divided by the "x"
// below is the Robots formula to determine each of their movements
double x = ( 2 * (log(place)/log(2))); <-------- now x is a double
return (int) x; <-------- now x is an integer, are you sure about that?
}
Loss of precision happens here. Let's say that x is assigned a value of 3.14. Then you cast it (the cast would happen automatically since the return type of the function is also an int) into an integer, thus it will be converted to 3, thus you loss precision.
About main.cpp
You call the user to input 1 and then you have:
while (userInput == 1) {
functionObj.position(4);
functionObj.print();
} //end while
but userInput is not going to be modified, thus you are running into a non-ending loop.

Filling 2-D arrays from user input

I have a bit of a problem, I am writing a program to ask the user to enter numbers for a Sudoku grid, and then store them in a 2-d array. I know how to print out the array to show the Sudoku grid, But I am having trouble getting the array elements set to the numbers that the user enters, can anyone help?
This is all that I have, which I know is not much but I have only ever done this with 1-d arrays before.
Code:
#include <iostream>
using namespace std;
void fillGrid1(int grid1, int sizeOfArray) {
for(int x = 0; x < sizeOfArray; x++) {
grid1[x][9] = x;
}
}
int main()
{
int grid1[9][9];
fillGrid1(grid1, 9);
for(int row = 0; row < 9; row++) {
for(int column = 0; column < 9; column++) {
cout << grid1[row][column] << " ";
}
cout << endl;
}
}
Here you have two functions, one to interactively fill the hole sudoku by getting the user input. The other for printing the sudoku. With the little information you gave it's what I think you seek:
#include <iostream>
#include <stdio.h>
#include<stdlib.h>
using namespace std;
void interactiveSudokuFill(int grid1[9][9]){
for(int y=0;y<9;y++){
for(int x=0;x<9;x++){
string theString;
cout<<"Write the value to prace in Sudoku["<<y<<"]["<<x<<"] :"<<endl;
std::getline(cin,theString);
int nr=atoi(theString.c_str());
grid1[y][x]=nr;
}
}
}
void printSudoku(int grid[9][9]){
for(int y=0;y<9;y++){
for(int x=0;x<9;x++){
cout<<"["<<grid[y][x]<<"]";
}
cout<<endl;
}
}
int main()
{
int grid1[9][9];
interactiveSudokuFill(grid1);
printSudoku(grid1);
}
There are other more safe/elegant ways of doing this(for example user input should have been checked before delievering it to atoi()), but this way is the simpler I can think of.
Firstly, you're taking in an int where you expect an array:
void fillGrid1(int grid1, int sizeOfArray)
// ^^^^^^^^^
This should be something of the form,
void fillGrid1(int grid1[9][9], int sizeOfArray)
Next is that you should use a nested loop to access the elements of the multidimensional array:
void fillGrid1(int grid1[9][9], int sizeOfArray)
{
for (int i = 0; i < sizeOfArray; ++i)
{
for (int k = 0; k < sizeOfArray; ++k)
{
grid1[i][k] = x; // shouldn't x be the number the user entered?
}
}
}
You should also zero-fill your array:
int grid1[9][9] = {0};