   Newbee with a programing question
  Hey all, i was hoping i could get some helpim working on a tic  tac toe game in Cim using an array so i can get that board ro display, and i know how to place the user''s X in a slot, but  not the  computer''s Oi figure i need a random function, but dont know how to work itif i could get a simple explanation/example that would be  great, thanks(keep it watered down cuase im a tad dumb on these things=]   ) 
  Hey ther dude!!!  Its very simple, use de rand () function, you use like this:  int x = rand()%MAX_N;  X will be a randon number between 0 and MAX_N;  I can't remember the library, I think it's stdlib.h!  I dont like this function very much, you'll see why. But It helps a lot...Was that what you need?   [edited by - Dogh Quch on February 13, 2004 1:47:22 PM];  rand() should be in stdlib.h by the way, and also, if your new, you may not know what the "%" operator does its called the modulous operator, or the maude (well i call it that :-D maybe some other ppl do) and it works like this(NOT CODE)[x]%[y] = remainder of x/y  if x is == to y, then the expression will be equal to zeroheres a few examples9%817%7014%705%3232%8031%87hope that wasnt all just rambling to yah, hope it helps ;  maybe some of this code will help #include <cstdlib>#include <ctime>// Seeds the random number generator.void randomize(void){srand((unsigned int)time(0));}int randomNumber(void){return (rand() % numbersgohereforrandom)} [edited by - mullink on February 13, 2004 2:30:19 PM];Caveat: rand() isn''t very random, and choosing only the low bits (by using modulus and a small number) makes it even less so. It''s sufficient for your needs at this point, but keep that in mind for later.Good luck. ;  wow...i get answers within   ours, and answers that are good and that i can understand!thanks a bundle all   Drogla Dreglo ;  Welcome to GDNet, Drogla...- CD   Brought to you by Code_Dark;  Okay Drogla- I have an old Tic Tac Toe program, and although I can''t find the final source, I have some beta-phase source that works... here ya go: #include <iostream.h>#include <stdlib.h>#include <conio.h>char matrix[3][3];char go;int choiceRow;int choiceCol;int compChoice;int x;bool won;bool squareOneR1;bool squareTwoR1;bool squareThreeR1;bool squareOneR2;bool squareTwoR2;bool squareThreeR2;bool squareOneR3;bool squareTwoR3;bool squareThreeR3;void startup(void) {bool won = false;bool squareOneR1 = false;bool squareTwoR1 = false;bool squareThreeR1 = false;bool squareOneR2 = false;bool squareTwoR2 = false;bool squareThreeR2 = false;bool squareOneR3 = false;bool squareTwoR3 = false;bool squareThreeR3 = false; system("cls"); cout << "\nWelcome to TIC-TAC-TOE!" << endl; cout << "Would you like to play now? (Y/N): "; cin >> go;if (go != ''y'' && go != ''Y'') { system("cls"); cout << "\nThanks for playing...\n" << endl; system("pause"); system("exit");}}void dispMatrix(void) {//the play boxsystem("cls"); //clears DOS box firstcout<<"\n\t\t 1   2   3\n"<<endl;cout<<"\t\t   1  "<<matrix[0][0]<<" | "<<matrix[0][1]<<" | "<<matrix[0][2]<<endl;cout<<"\t\t---|---|---\n";cout<<"\t\t   2  "<<matrix[1][0]<<" | "<<matrix[1][1]<<" | "<<matrix[1][2]<<endl;cout<<"\t\t---|---|---\n";cout<<"\t\t   3  "<<matrix[2][0]<<" | "<<matrix[2][1]<<" | "<<matrix[2][2]<<"\n\n\n";}void askUser(void) { //ask user choice cout << "Enter the ROW: "; cin >> choiceRow; cout << "\nEnter the COLUMN: "; cin >> choiceCol;}void setChoice(void) { //sets the user''s choice into the matrix matrix[(choiceRow - 1)][(choiceCol - 1)] = ''X'';}void compMove(void) { if (won == false) {if (matrix[1][1] != ''X'' && matrix[1][1] != ''O'') { matrix[1][1] = ''O'';}else if (matrix[2][2] != ''X'' && matrix[2][2] != ''O'') { matrix[2][2] = ''O'';}else if (matrix[2][2] != ''X'' && matrix[2][2] != ''O'') { matrix[2][2] = ''O'';}else if (matrix[2][0] != ''X'' && matrix[2][0] != ''O'') { matrix[2][0] = ''O'';}else if (matrix[0][0] != ''X'' && matrix[0][0] != ''O'') { matrix[0][0] = ''O'';}else if (matrix[0][2] != ''X'' && matrix[0][2] != ''O'') { matrix[0][2] = ''O'';}else if (matrix[0][1] != ''X'' && matrix[0][1] != ''O'') { matrix[0][1] = ''O'';}else if (matrix[1][0] != ''X'' && matrix[1][0] != ''O'') { matrix[2][2] = ''O'';}else if (matrix[1][2] != ''X'' && matrix[1][2] != ''O'') { matrix[1][2] = ''O'';}}}void checkWin(void) { if ((matrix[0][0] == ''X'' && matrix[0][1] == ''X'' && matrix[0][2] == ''X'') ||(matrix[0][0] == ''X'' && matrix[1][1] == ''X'' && matrix[2][2] == ''X'') ||(matrix[0][0] == ''X'' && matrix[1][0] == ''X'' && matrix[2][0] == ''X'') ||(matrix[1][0] == ''X'' && matrix[1][1] == ''X'' && matrix[1][2] == ''X'') ||(matrix[2][0] == ''X'' && matrix[2][1] == ''X'' && matrix[1][2] == ''X'') ||(matrix[2][0] == ''X'' && matrix[1][1] == ''X'' && matrix[0][2] == ''X'') ||(matrix[0][1] == ''X'' && matrix[1][1] == ''X'' && matrix[2][1] == ''X'') ||(matrix[0][2] == ''X'' && matrix[1][2] == ''X'' && matrix[2][2] == ''X'')) {  cout << "\nCongratulations, you won! :)" << endl;  system("pause");  won = true;}  else if ((matrix[0][0] == ''O'' && matrix[0][1] == ''O'' && matrix[0][2] == ''O'') ||(matrix[0][0] == ''O'' && matrix[1][1] == ''O'' && matrix[2][2] == ''O'') ||(matrix[0][0] == ''O'' && matrix[1][0] == ''O'' && matrix[2][0] == ''O'') ||(matrix[1][0] == ''O'' && matrix[1][1] == ''O'' && matrix[1][2] == ''O'') ||(matrix[2][0] == ''O'' && matrix[2][1] == ''O'' && matrix[1][2] == ''O'') ||(matrix[2][0] == ''O'' && matrix[1][1] == ''O'' && matrix[0][2] == ''O'') ||(matrix[0][1] == ''O'' && matrix[1][1] == ''O'' && matrix[2][1] == ''O'') ||(matrix[0][2] == ''O'' && matrix[1][2] == ''O'' && matrix[2][2] == ''O'')) {  cout << "\nSorry, you lost! :(" << endl;  won = true;  system("pause");}  else {   cout << "\nNo Winner..." << endl;   won = false;  }}int main() { startup(); do {    dispMatrix();    askUser();    setChoice();    checkWin();    compMove();    checkWin();    dispMatrix(); } while (won == false); system("cls"); cout << "\n\tThanks for playing DSHAW Tic-Tac-Toe :)\n\n" << endl; system("pause"); system("exit");}You can download the current (final) build here (exe file).- CD   Brought to you by Code_Dark;  I made a tic-tac-toe when I read this post. It's not any good, the ai is pretty nonexistent(it only chooses a place at random), but I made it in about half an hour.tic.h    #pragma once#include <vector>class TicTacToe{public:TicTacToe();bool PlayerTurn();bool AITurn();private:void DrawBoard() const;void Help() const;bool CheckForThree() const;std::vector<int> placed;};tic.cpp #include <iostream>#include <sstream>#include <cstdlib>#include "tic.h"using namespace std;TicTacToe::TicTacToe(){placed.insert(placed.begin(), 9, 0);}void TicTacToe::Help() const{cout << "-------------\n| " << 0 << " | " << 1 << " | " << 2 <<" |\n-------------\n| " << 3 << " | " << 4 << " | " << 5 <<" |\n-------------\n| " <<6 << " | " << 7 << " | " << 8 << " |\n-------------\n";cout << "\nSkriv siffran p\206 den plats du vill s\204tta ditt kryss:\n\n" << endl;}void TicTacToe::DrawBoard() const{cout << "-------------\n| " << (placed.at(0) == 0 ? " " : placed.at(0) == 1 ? "x" : "o") <<" | " << (placed.at(1) == 0 ? " " : placed.at(1) == 1 ? "x" : "o") <<" | " << (placed.at(2) == 0 ? " " : placed.at(2) == 1 ? "x" : "o") <<" |\n-------------\n| " <<(placed.at(3) == 0 ? " " : placed.at(3) == 1 ? "x" : "o") <<" | " << (placed.at(4) == 0 ? " " : placed.at(4) == 1 ? "x" : "o") <<" | " << (placed.at(5) == 0 ? " " : placed.at(5) == 1 ? "x" : "o") <<" |\n-------------\n| " <<(placed.at(6) == 0 ? " " : placed.at(6) == 1 ? "x" : "o") <<" | " << (placed.at(7) == 0 ? " " : placed.at(7) == 1 ? "x" : "o") <<" | " << (placed.at(8) == 0 ? " " : placed.at(8) == 1 ? "x" : "o") <<" |\n-------------\n";}bool TicTacToe::PlayerTurn(){Help();DrawBoard();cout << "Var vill du s\204tta ditt kryss? <valfri bokstav f\224r att avsluta> ";int p;if (!(std::cin >> p)){exit(0);}else if (placed.at(p) == 0){placed.at(p) = 1;}else{PlayerTurn();}return CheckForThree();}bool TicTacToe::AITurn(){int p = rand() % 9;if (placed.at(p) == 0){placed.at(p) = 2;}else{AITurn();}return CheckForThree();}bool TicTacToe::CheckForThree() const{if ((placed.at(0) == 1 && placed.at(1) == 1 && placed.at(2) == 1) ||(placed.at(3) == 1 && placed.at(4) == 1 && placed.at(5) == 1) ||(placed.at(6) == 1 && placed.at(7) == 1 && placed.at(8) == 1) ||(placed.at(0) == 1 && placed.at(3) == 1 && placed.at(6) == 1) ||(placed.at(1) == 1 && placed.at(4) == 1 && placed.at(7) == 1) ||(placed.at(2) == 1 && placed.at(5) == 1 && placed.at(8) == 1) ||(placed.at(0) == 1 && placed.at(4) == 1 && placed.at(8) == 1) ||(placed.at(2) == 1 && placed.at(4) == 1 && placed.at(6) == 1)){system("cls");Help();DrawBoard();cout << "Du vann!" << endl;return true;}else if ((placed.at(0) == 2 && placed.at(1) == 2 && placed.at(2) == 2) ||(placed.at(3) == 2 && placed.at(4) == 2 && placed.at(5) == 2) ||(placed.at(6) == 2 && placed.at(7) == 2 && placed.at(8) == 2) ||(placed.at(0) == 2 && placed.at(3) == 2 && placed.at(6) == 2) ||(placed.at(1) == 2 && placed.at(4) == 2 && placed.at(7) == 2) ||(placed.at(2) == 2 && placed.at(5) == 2 && placed.at(8) == 2) ||(placed.at(0) == 2 && placed.at(4) == 2 && placed.at(8) == 2) ||(placed.at(2) == 2 && placed.at(4) == 2 && placed.at(6) == 2)){system("cls");Help();DrawBoard();cout << "Du f\224rlorade!" << endl;return true;}else{return false;}}main.cpp #include <cstdlib>#include <ctime>#include "tic.h"int main(int argc, char *argv[]){using namespace std;srand(time(0));TicTacToe board;bool done = false;while (!done){done = board.PlayerTurn();if (!done){done = board.AITurn();}done == false ? system("cls") : 1;}return 0;}I'll probably fix the ai tomorrow. [edited by - Eriond on February 13, 2004 4:58:33 PM];  And that''s C, not C++, right? Hehe, I''m such a hypocrite. And btw, #pragma is deprecated.- CD   Brought to you by Code_Dark