• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
phil67rpg

more tic tac toe

31 posts in this topic

I am still working on my tic tac toe game using c++.What I want to do is check an array of characters for X's or O's.If it finds an X or O then it generates another O.It checks the array until it finds an empty spot and then puts an O in that spot.
here is the code I am working on.
[code]
if(board[player_O]!='X' && board[player_O]!='O')
{
board[player_O]='O';
}
else
{
player_O_two=rand()%9+1;
}

if(player_O_two==player_O)
{
player_O=rand()%9+1;
board[player_O]='O';
}
[/code]
I only need a small hint.
0

Share this post


Link to post
Share on other sites
I don't understand the description in English of what you are trying to do. Perhaps you can try to explain it with an example?
0

Share this post


Link to post
Share on other sites
ok I am trying to check for a empty spot on a tic tac toe board and then put an X or O in that spot.I dont want the computer to put a O in an already occupied spot.
0

Share this post


Link to post
Share on other sites
One reasonable method consist of making a list of the empty spaces, then pick a random one from those.

Do you need to see code for that?
0

Share this post


Link to post
Share on other sites
There you go:
[code]#include <iostream>
#include <cstdlib>
#include <ctime>
#include <vector>

enum Piece {Empty, Cross, Nought};

class Board {
Piece array[9];

public:
Board() {
for (int i=0; i<9; ++i)
array[i] = Empty;
}

void make_move(int square, Piece piece) {
array[square] = piece;
}

int pick_random_empty_square() {
std::vector<int> empties;
empties.reserve(9);
for (int i=0; i<9; ++i) {
if (array[i] == Empty)
empties.push_back(i);
}

int random_index = std::rand() % empties.size();

return empties[random_index];
}

friend std::ostream &operator<<(std::ostream &os, Board const &board);
};

std::ostream &operator<<(std::ostream &os, Board const &board) {
for (int row=0; row<3; ++row) {
for (int col=0; col<3; ++col)
os << "_XO"[board.array[3*row+col]] << ' ';
os << '\n';
}
return os;
}

int main() {
std::srand(std::time(0));

Board board;

for (int i=0; i<9; ++i) {
board.make_move(board.pick_random_empty_square(), i%2==0 ? Cross : Nought);
std::cout << board << '\n';
}
}
[/code]
0

Share this post


Link to post
Share on other sites
If all you're actually trying to do is find the first empty space and replace it with an O then this would do it:

[CODE]

//Set all spaces to ' ' when you initialize the board
for (int i = 0; i < 8; ++i) //Where 8 is the size of the board
{
board[i] = ' ';
}

//Then when you're wanting to replace the first empty space wth 'O'
for (int i = 0; i < 8; ++i)
{
if (board[i] == ' ')
board[i] = 'O';
}
[/CODE]
0

Share this post


Link to post
Share on other sites
[quote name='littletray26' timestamp='1350595688' post='4991555']
If all you're actually trying to do is find the first empty space and replace it with an O then this would do it:
[CODE]
//Set all spaces to ' ' when you initialize the board
for (int i = 0; i < 8; ++i) //Where 8 is the size of the board
{
board[i] = ' ';
}
//Then when you're wanting to replace the first empty space wth 'O'
for (int i = 0; i < 8; ++i)
{
if (board[i] == ' ')
board[i] = 'O';
}
[/CODE]
[/quote]


if you do not put a break in that if statement, it will change ALL empty spaces into O's, rather than the first one you find

If (board[i] =='')
{
board [i] = 'O';
break;
}

that way only one empty is changed

also i think you may want 9 instead of 8; arent there 9 tiles in TTT? Edited by arkane7
2

Share this post


Link to post
Share on other sites
[quote name='arkane7' timestamp='1350663775' post='4991813']
[quote name='littletray26' timestamp='1350595688' post='4991555']
If all you're actually trying to do is find the first empty space and replace it with an O then this would do it:
[CODE]
//Set all spaces to ' ' when you initialize the board
for (int i = 0; i < 8; ++i) //Where 8 is the size of the board
{
board[i] = ' ';
}
//Then when you're wanting to replace the first empty space wth 'O'
for (int i = 0; i < 8; ++i)
{
if (board[i] == ' ')
board[i] = 'O';
}
[/CODE]
[/quote]


if you do not put a break in that if statement, it will change ALL empty spaces into O's, rather than the first one you find

If (board[i] =='')
{
board [i] = 'O';
break;
}

that way only one empty is changed

also i think you may want 9 instead of 8; arent there 9 tiles in TTT?
[/quote]

You're right, I forgot to put that break in there. Thank you for correcting that :)

Also, 0 is counted as a space so in an 8 space array there is in fact 9 spaces.[quote name='arkane7' timestamp='1350663775' post='4991813']
[quote name='littletray26' timestamp='1350595688' post='4991555']
If all you're actually trying to do is find the first empty space and replace it with an O then this would do it:
[CODE]
//Set all spaces to ' ' when you initialize the board
for (int i = 0; i < 8; ++i) //Where 8 is the size of the board
{
board[i] = ' ';
}
//Then when you're wanting to replace the first empty space wth 'O'
for (int i = 0; i < 8; ++i)
{
if (board[i] == ' ')
board[i] = 'O';
}
[/CODE]
[/quote]


if you do not put a break in that if statement, it will change ALL empty spaces into O's, rather than the first one you find

If (board[i] =='')
{
board [i] = 'O';
break;
}

that way only one empty is changed

also i think you may want 9 instead of 8; arent there 9 tiles in TTT?
[/quote]
0

Share this post


Link to post
Share on other sites
[quote name='phil67rpg' timestamp='1350856170' post='4992580']
is there anyway I can generate a random number only once and do not have it repeat.
[/quote]
If you mean produce a sequence of numbers where no number is repeated, like 3, 1, 9, 5, 2, 4, ... then sure, it's very easy:

1. generate an array [0, 1, 2, 3, 4, ... as much as you need, pretty much put the numbers you want in there]
2. for each element A in the array...
----> select a random element B in the array
----> swap elements A and B
3. iterate through the modified array to get your repeat-free random numbers

See [url="http://en.wikipedia.org/wiki/Fisher–Yates_shuffle"]Fisher-Yates shuffle[/url]. Is this what you meant? Edited by Bacterius
2

Share this post


Link to post
Share on other sites
Note the algorithm I posted is the naive, inefficient one. The correct one is given on Wikipedia and is much faster:

[source lang="plain"]To shuffle an array a of n elements (indices 0..n-1):
for i from n ? 1 downto 1 do
j ? random integer with 0 ? j ? i
exchange a[j] and a[i][/source]
2

Share this post


Link to post
Share on other sites
[quote name='littletray26' timestamp='1350595688' post='4991555']
Also, 0 is counted as a space so in an 8 space array there is in fact 9 spaces.
[/quote]

How can there be 9 spaces in an array of size 8? That makes no sense.

Arkane7 was correct. You will need to change that 8 to a 9. Otherwise, you can change the < sign to <= .
0

Share this post


Link to post
Share on other sites
well i have found an algorithm that might work
[code]
#include <iostream>
#include <ctime>
using namespace std;
bool isContained(int arr[],int n,int x)
{
for(int i=0;i<n;i++)
{
if(arr[i]==x)
return true;
}
return false;
}
int main()
{
srand(time(0));
const int x = 100;
int r[x];
for(int i=0;i<x;i++)
{
r[i]=1+rand()%x;
while(isContained(r,i,r[i]))
{
r[i]=1+rand()%x;
}
}
for(int i=0;i<x;i++)
cout<<r[i]<<"\n";
cout<<"\n\n";
return 0;
}
[/code]
0

Share this post


Link to post
Share on other sites
That is slow. I gave a link to the proper algorithm in my last post - why not use it? Since you want code:

[source lang="cpp"]#include < iostream>
#include < cstdlib>
#include < ctime>

using namespace std;

void fisherYates(int array[], int length)
{
/* Go over each element backwards. */
for (int t = length - 1; t != 0; t--)
{
/* Select random element in [0..t] */
int r = rand() % (t + 1);

/* Swap them. */
int tmp = array[t];
array[t] = array[r];
array[r] = tmp;
}
}

int main()
{
/* Randomize. */
srand(time(0));

/* Prepare array of N elements. */
const int length = 20;
int array[length];

/* Initialize it. */
for (int t = 0; t < length; ++t) array[t] = t;

/* Shuffle. */
fisherYates(array, length);

/* Print out array. */
for (int t = 0; t < length; ++t) cout < < array[t] < < " ";
cout < < endl;
}[/source]

(remove spaces between << as the source tags munch'em apparently) Edited by Bacterius
1

Share this post


Link to post
Share on other sites
[sub][size=3]I have tried fisherYates shuffle but it still does not work.here is my code[/size][/sub]

[sub][size=3]void Computer::move_player_O()
{
srand(time(NULL)); [/size][/sub]
[sub][size=3]const int length=10;
int array[length];
for(int t=1;t<=9;t++)
array[t]=t;
fisherYates(array,length);
player_O=array[t];
t++;
}[/size][/sub]
[sub][size=3] [/size][/sub]
[sub][size=3]void Computer::check_player_O()
{
board[player_O]='O';
}[/size][/sub]
0

Share this post


Link to post
Share on other sites
Why are you incrementing t again inside the loop... the t++ inside the for construct already takes care of that :(
2

Share this post


Link to post
Share on other sites
The code doesn't actually make sense. player_O=array[t]; specifically - once the array is shuffled, you want to pick each element one after the other to get a list of numbers which don't repeat. Obviously this means you need to store the shuffled array somewhere, and use it for all 9 numbers (or 10.. why is it 10?) because if you just generate a new array each time and pick one element from this array, well, you've basically done nothing. Edited by Bacterius
0

Share this post


Link to post
Share on other sites
[quote name='dAND3h' timestamp='1350857927' post='4992596']
How can there be 9 spaces in an array of size 8? That makes no sense.
[/quote]

If we have for example
[code]
int banana[5];
[/code]

There is in fact 6 spaces.

[code]
banana[0] = 1;
banana[1] = 2;
banana[2] = 3;
banana[3] = 4;
banana[4] = 5;
banana[5] = 6;
[/code]

Arrays start off at zero, so in an 8 space array there is 9 spaces.
0

Share this post


Link to post
Share on other sites
[quote name='dAND3h' timestamp='1350857927' post='4992596']
Otherwise, you can change the < sign to <= .
[/quote]

This was the confusion. Sorry.
0

Share this post


Link to post
Share on other sites
[quote name='littletray26' timestamp='1350913849' post='4992764']
[quote name='dAND3h' timestamp='1350857927' post='4992596']
How can there be 9 spaces in an array of size 8? That makes no sense.
[/quote]

If we have for example
[code]
int banana[5];
[/code]

There is in fact 6 spaces.

[code]
banana[0] = 1;
banana[1] = 2;
banana[2] = 3;
banana[3] = 4;
banana[4] = 5;
banana[5] = 6;
[/code]

Arrays start off at zero, so in an 8 space array there is 9 spaces.
[/quote]

No, that's not how it works. Array indices do start at zero, so in an array of size 8, the last valid entry has index 7.
0

Share this post


Link to post
Share on other sites
[quote name='littletray26' timestamp='1350913849' post='4992764']
[quote name='dAND3h' timestamp='1350857927' post='4992596']
How can there be 9 spaces in an array of size 8? That makes no sense.
[/quote]

If we have for example
[code]
int banana[5];
[/code]

There is in fact 6 spaces.

[code]
banana[0] = 1;
banana[1] = 2;
banana[2] = 3;
banana[3] = 4;
banana[4] = 5;
banana[5] = 6;
[/code]

Arrays start off at zero, so in an 8 space array there is 9 spaces.
[/quote]

See Alvaro's response. But please try to take advice onboard , or at least make sure you are correct before you end up giving wrong advice. I probably should have explained it when I said it. Alas, No harm done [img]http://public.gamedev.net//public/style_emoticons/default/smile.png[/img] Edited by dAND3h
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0