mardi 7 mai 2019

Generating two random numbers or selecting two random spots from an array

I am currently learning c++ and am trying to create a battleship game against a computer. I am trying to generate a random spot from an array but it is giving me an error.

This is what I have so far

#include<iostream>
#include<ctime>
#include<cstdlib>
using namespace std;
int player[8][8], comp[8][8];
int num_ships;
int x, y, z;//these change often, are just placeholder

void gen()
{
    for (int z = 0; z <= num_ships; z++)//num_ships comes from somewhere else
    {
        x = srand(time(0) % 8);
        y = srand(time(0) % 8);

        if (comp[x][y] = 1)
        {
            --num_ships;//forces another try
        }
        else
        {
            comp[x][y] = 1;//1 is with a ship in this case
        }
    }
}

I want x and y to be different numbers as well, is there a way to do that? (or can i just do:)

(srand(time(0))+srand(time(0)))%8

but in the line,

x = srand(time(0) % 8);

it gives an error for "a value of type void cannot be assigned to an entity of type int"

Thanks for your help.




Aucun commentaire:

Enregistrer un commentaire