0 Replies - 6302 Views - Last Post: 30 December 2011 - 03:30 PM

#1 ssharma286  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 93
  • Joined: 04-December 10

Implementing a GUI to sudoku game

Posted 30 December 2011 - 03:30 PM

Hello I am trying to build a sudoku game. Previously I made this application in C++ but now I have decided to add a GUI. I have never worked with CoaCoa and Objective-C before. After hours of painstaking research I managed to change my C++ functions and class to Objective-C. But now I am stuck on how I can make the game appear on the GUI. I have posted the files I have and the GUI I have made. I am not sure where to put the code to create a game and how to print my created game to the GUI.

To start a game I need to -
create two instances of board -> problem, solution.
Call [problem create];
Call [solution create:problem.sudo];

Image of my GUI
Posted Image
NOTE: They are all textfields.
My board header file:
#import <Foundation/Foundation.h>

const int ROWS = 9;
const int COLS = 9;
@interface board : NSObject {
  char sudo[9][9];
  IBOutlet id textField[9][9]; // my fail attempt
}
-(bool) attemptCreate;
-(bool) checkRow:(char) value: (int) ROW;
-(bool) checkCol:(char) value:(int) COL;
-(bool) checkBox:(char) value: (int) boxNum;
-(bool) validChoice:(char) value:(int) ROW: (int) COL;
-(int) boxNum:(int) ROW: (int) COL;
-(void) clear;
-(id)init;
-(void) create;
-(bool)win: (char**) solution;
-(void)create:(char**) solution;
@end



My board implementation file:
#import "board.h"

@implementation board
- (bool) attemptCreate{
  [self clear];
  for(int i = 0; i < ROWS; i++){
    for(int j = 0; j < COLS; j++){
      int numOfTries = 0;
      while(numOfTries != 30){
        char insert = (rand() % 9) + 49;
        numOfTries++;
        if([self validChoice:insert: i: j]){
          sudo[i][j] = insert;
          break;
        }
      }
      if(numOfTries == 30)
        return false;
    }
  }
  return true;
}
- (bool)checkRow:(char)value:(int) ROW{
  for(int i = 0; i < ROWS; i++)
    if(sudo[ROW][i] == value)
      return TRUE;
  return FALSE;
}
- (bool) checkCol:(char) value:(int) COL{
  for(int i = 0; i < COLS; i++)
    if(sudo[i][COL] == value)
      return true;
  return false;
}
- (bool) checkBox:(char) value:(int) boxNum{
  bool result = false;
  int startRow, startCol;
  if(boxNum  < 3)
    startRow = 0;
  else if(boxNum >= 3 && boxNum < 6)
    startRow = 3;
  else if(boxNum >= 6 && boxNum < 9)
    startRow = 6;
  
  
  for(int i = 0; i != boxNum+1; i++){
    if(i % 3 == 0 || i == 0) startCol= 0;
    else startCol += 3;
  }
  
  for(int i = startRow; i < (startRow + 3); i++){
    for(int j = startCol; j < (startCol + 3); j++){
      if(sudo[i][j] == value)
        result = true;
    }
  }
  return result;
}
- (bool)validChoice:(char) value:(int)ROW:(int) COL {
  bool result = false;
  result = ![self checkBox:value:[self boxNum:ROW:COL]];
  if(result) result = ![self checkCol:value:COL];
  if(result) result = ![self checkRow:value:ROW];

  return result;
}
- (int) boxNum:(int) ROW: (int) COL{
  int boxNum = 0;
  if(ROW >= 3 && ROW < 6)
    boxNum = 3;
  else if(ROW >= 6 && ROW < 9)
    boxNum = 6; 
  
  if(COL >= 3 && COL < 6)
    boxNum += 1;
  else if(COL >= 6 && COL < 9)
    boxNum += 2;  
  
  
  return boxNum;
}
- (void) clear{
  for(int i = 0; i < ROWS; i++)
    for(int j = 0; j < COLS; j++)
      sudo[i][j] = ' ';
}
-(id)init{
  [self clear];
  return self;
}
-(void)create{
  while(![self attemptCreate]);
}
-(bool)win:(char**) solution{
  for(int i = 0; i < ROWS; i++)
    for(int j = 0; j < COLS; j++)
      if(sudo[i][j] != solution[i][j])
        return false;
  return true;
  
}
-(void)create:(char**) solution{
  for(int i = 0; i < 20; i++){
    int randRow = rand() % 9;
    int randCol = rand() % 9;
    sudo[randRow][randCol] = solution[randRow][randCol];
  }
  
}
@end


This post has been edited by ssharma286: 30 December 2011 - 10:52 PM


Is This A Good Question/Topic? 0
  • +

Page 1 of 1