c++ grid help

Discussion in 'Programming' started by izlik, Sep 14, 2009.

  1. #1
    Hello Bellow i have a C++ grid code im using for a programm

    --------
    |X|X|X
    |X|X|X
    |X|X|X
    --------
    The output is like above where X is a random number put in by the "rand() % 3 + 1". now, my question is this. when the everyting is put out on the screen and numbers are in each box, how can i check to se if there is 3 of the same number in line with each other ??


    int grid[3][3]; // grid
    			grid[0][0] = rand() % 3 + 1; //Randomiserar
    			grid[0][1] = rand() % 3 + 1; //Randomiserar 
    			grid[0][2] = rand() % 3 + 1; //Randomiserar 
    
    			grid[1][0] = rand() % 3 + 1; //Randomiserar
    			grid[1][1] = rand() % 3 + 1; //Randomiserar 
    			grid[1][2] = rand() % 3 + 1; //Randomiserar 
    
    			grid[2][0] = rand() % 3 + 1; //Randomiserar 
    			grid[2][1] = rand() % 3 + 1; //Randomiserar 
    			grid[2][2] = rand() % 3 + 1; //Randomiserar 
    
    			cout << "-Line1--Line2---Line3-" << endl; 
    
    			for (int col = 0; col < 3; col ++)           
    			{
    
    				for (int row = 0; row < 3; row ++)
    				{
    					cout << "|  " << grid[row][col] << "  |";
    Code (markup):
     
    izlik, Sep 14, 2009 IP
  2. LogicFlux

    LogicFlux Peon

    Messages:
    2,925
    Likes Received:
    102
    Best Answers:
    0
    Trophy Points:
    0
    #2
    I think you can do something like the following. I'm assuming tic tac toe style rules where diagonals count?

    
    
       int matches[8][3][2] = {{{0,0}, {1,0}, {2,0}}, {{0,0}, {0,1}, {0,2}}, {{0,0}, {1,1}, {2,2}},\
      {{0,1}, {1,1}, {2,1}}, {{0,2}, {1,1}, {2,0}}, {{0,2}, {1,2}, {2,2}}, {{1,0}, {1,1}, {1,2}},\
      {{2,0}, {2,1}, {2,2}}};
      
      int tmpTrips[3];
      int rowIndex, colIndex;
      
      int i, j, k;
      for (i=0; i<8; i++)
      { 
        for (j=0; j<3; j++)
        {
          for (k=0; k<2; k++)
          {
            if (k==0) rowIndex = matches[i][j][k];
            else colIndex = matches[i][j][k];
          }
          
          tmpTrips[j] = grid[rowIndex][colIndex];
          
        }
        
        if (tmpTrips[0] == tmpTrips[1] && tmpTrips[1] == tmpTrips[2])
        {
           printf("Three in a row!\n");
           break;
        }
    
      }
    
    Code (markup):
    Not completely tested because I didn't feel like building the grid to fully test it. But it should be at least close.
     
    LogicFlux, Sep 14, 2009 IP
  3. izlik

    izlik Well-Known Member

    Messages:
    2,399
    Likes Received:
    50
    Best Answers:
    0
    Trophy Points:
    185
    #3
    Ye it was someting like that i was looking for! thank you, however im having really hard problems getting it to work with my code :( im adding the grid i have.

    #include "stdafx.h"
    #include <iostream>
    #include <ctime>
    
    using namespace std;
    int main()
    {
    	//Deklarera variabler
    	int j = 0, g = 0, h = 0, cpu, pengar = 0, vinst2, satsa, drabort, forlust, quit;
    	srand(time(0));
    	while(true){
    		//om pengar är 0 startar spelet här, om pengar blir 0 under spelets gång börjar spelet om här
    		if (pengar == 0)
    		{
    			//välkommnar spelaren samt frågar hur mycket han vill sätta in.
    			cout << "how much do you wanna transfer from your bank? :"; cin >> pengar;
    			cout << endl;
    
    			//Random funktion för allas tärningar
    	
    			int grid[3][3]; // grid
    			grid[0][0] = rand() % 3 + 1; //Randomiserar
    			grid[0][1] = rand() % 3 + 1; //Randomiserar 
    			grid[0][2] = rand() % 3 + 1; //Randomiserar 
    
    			grid[1][0] = rand() % 3 + 1; //Randomiserar
    			grid[1][1] = rand() % 3 + 1; //Randomiserar 
    			grid[1][2] = rand() % 3 + 1; //Randomiserar 
    
    			grid[2][0] = rand() % 3 + 1; //Randomiserar 
    			grid[2][1] = rand() % 3 + 1; //Randomiserar 
    			grid[2][2] = rand() % 3 + 1; //Randomiserar 
    
    			cout << "-Line1--Line2---Line3-" << endl; 
    
    			for (int col = 0; col < 3; col ++)           
    			{
    
    				for (int row = 0; row < 3; row ++)
    				{
    					cout << "|  " << grid[row][col] << "  |";
    
    [quote="LogicFlux, post: 12475613"]I think you can do something like the following. I'm assuming tic tac toe style rules where diagonals count?
    Code (markup):
    
    
       int matches[8][3][2] = {{{0,0}, {1,0}, {2,0}}, {{0,0}, {0,1}, {0,2}}, {{0,0}, {1,1}, {2,2}},\
      {{0,1}, {1,1}, {2,1}}, {{0,2}, {1,1}, {2,0}}, {{0,2}, {1,2}, {2,2}}, {{1,0}, {1,1}, {1,2}},\
      {{2,0}, {2,1}, {2,2}}};
      
      int tmpTrips[3];
      int rowIndex, colIndex;
      
      int i, j, k;
      for (i=0; i<8; i++)
      { 
        for (j=0; j<3; j++)
        {
          for (k=0; k<2; k++)
          {
            if (k==0) rowIndex = matches[i][j][k];
            else colIndex = matches[i][j][k];
          }
          
          tmpTrips[j] = grid[rowIndex][colIndex];
          
        }
        
        if (tmpTrips[0] == tmpTrips[1] && tmpTrips[1] == tmpTrips[2])
        {
           printf("Three in a row!\n");
           break;
        }
    
      }
    
    Code (markup):
    Not completely tested because I didn't feel like building the grid to fully test it. But it should be at least close.[/QUOTE]
     
    izlik, Sep 14, 2009 IP