[Home]Kazuhiko/Game-Sudoku

ec2-18-118-200-86.us-east-2.compute.amazonaws.com | ToothyWiki | Kazuhiko | RecentChanges | Login | Webcomic

[Click to play]

Sudoku puzzles implemented in ToothyGDL.

A triumph of technical-can do over actual playability.  The puzzle is fully functional and checks if you have a correct solution.  It is, however, almost unplayable due to the lack of borders between sub squares and, in higher levels, the lack of ability to make notes without really messing up your monitor...



To Do:



ToothyGDL: [Recompile] - [View compiler errors]

logic
{
  // Puzzles are listed in the initial puzzles array
  // First a number between 1 and 5 to indicate difficulty and then the 9*9 grid
  // Build the grid using 1-9 and 0 to represent an empty space
  const int numPuzzles { 7 }
  const array puzzles
    {
      1,
      0,4,0,3,0,0,6,1,0,
      1,2,3,0,0,0,0,0,9,
      0,0,0,7,8,0,2,0,0,
      4,0,0,0,7,2,5,0,8,
      0,0,9,6,0,5,3,0,0,
      2,0,6,8,1,0,0,0,7,
      0,0,2,0,9,4,0,0,0,
      9,0,0,0,0,0,7,5,3,
      0,6,8,0,0,7,0,4,0,
      
      2,
      0,0,0,0,0,6,3,0,0,
      0,5,0,0,0,0,0,0,8,
      8,6,0,2,0,1,0,9,7,
      1,8,0,0,0,0,2,0,0,
      0,0,4,1,0,7,8,0,0,
      0,0,2,0,0,0,0,4,9,
      7,3,0,8,0,5,0,6,2,
      2,0,0,0,0,0,0,3,0,
      0,0,5,9,0,0,0,0,0,
      
      2,
      0,5,0,0,0,1,0,0,0,
      3,9,0,6,0,0,2,7,1,
      0,6,0,0,2,0,0,5,3,
      1,2,0,0,0,0,0,0,8,
      8,0,6,0,0,0,3,0,5,
      5,0,0,0,0,0,0,2,6,
      4,3,0,0,6,0,0,9,0,
      6,8,2,0,0,9,0,3,7,
      0,0,0,5,0,0,0,6,0,
      
      3,
      5,0,0,2,0,0,4,0,1,
      0,0,0,0,0,8,0,0,0,
      4,1,0,0,0,6,0,8,0,
      7,0,0,0,8,0,2,0,4,
      0,0,4,0,0,0,3,0,0,
      9,0,5,0,4,0,0,0,6,
      0,4,0,8,0,0,0,6,9,
      0,0,0,9,0,0,0,0,0,
      8,0,3,0,0,5,0,0,7,
      
      3,
      0,7,0,0,6,8,0,0,0,
      3,0,0,0,0,0,0,6,0,
      0,9,5,3,0,0,0,0,2,
      8,0,0,7,0,1,9,0,0,
      9,0,2,0,0,0,7,0,6,
      0,0,7,5,0,6,0,0,4,
      7,0,0,0,0,2,6,4,0,
      0,2,0,0,0,0,0,0,8,
      0,0,0,8,3,0,0,2,0,
      
      4,
      0,3,0,7,0,0,2,9,0,
      2,5,8,0,0,1,7,0,0,
      0,0,0,0,0,5,0,0,0,
      0,0,9,0,0,0,8,0,0,
      0,0,0,4,2,3,0,0,0,
      0,0,2,0,0,0,3,0,0,
      0,0,0,8,0,0,0,0,0,
      0,0,5,6,0,0,9,3,7,
      0,9,6,0,0,4,0,8,0,
      
      5,
      0,3,0,0,0,4,0,0,5,
      0,4,0,0,0,1,0,0,7,
      6,0,9,0,0,8,0,0,0,
      0,5,0,9,0,3,0,0,1,
      0,0,8,0,0,0,9,0,0,
      2,0,0,1,0,6,0,5,0,
      0,0,0,8,0,0,6,0,2,
      5,0,0,3,0,0,0,1,0,
      1,0,0,4,0,0,0,7,0,
    }
  
  array puzzle
    {
      0,0,0,0,0,0,0,0,0,
      0,0,0,0,0,0,0,0,0,
      0,0,0,0,0,0,0,0,0,
      0,0,0,0,0,0,0,0,0,
      0,0,0,0,0,0,0,0,0,
      0,0,0,0,0,0,0,0,0,
      0,0,0,0,0,0,0,0,0,
      0,0,0,0,0,0,0,0,0,
      0,0,0,0,0,0,0,0,0
    }
  
  int selectedPuzzle { -1 }
  int selectedDifficulty { 0 }
  int selectedNumber { 0 }
  
  // Variables used in testing for victory
  const array SubSquareStart { 0, 3, 6, 27, 30, 33, 54, 57, 60 }
  local int checkOn { 0 }
  local array numberCount { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
  local array puzzleComplete { 0 }
  
  // Allow puzzle to be chosen
  for i (0 .. numPuzzles-1)
  {
    when {selectedPuzzle == -1}
    allow
    {
      selectedPuzzle = i;
      selectedDifficulty = puzzles[i*(9*9+1)];
      selectedNumber = 0;
      for j (0 .. (9*9)-1)
      {
        puzzle[j] = puzzles[(i*(9*9+1)) + 1 + j];
        if (puzzle[j] == 0) {puzzle[j] = 10;}
      }
    }
    label {"Picked puzzle " i}
  }
  
  // Allow number to be selected
  for i (0 .. 9)
  {
    when {selectedPuzzle != -1}
    allow
    {
      selectedNumber = i;
    }
    label {"Selected " i}
  }
  
  // Allow number to be placed
  for i (0 .. (9*9)-1)
  {
    when {(selectedPuzzle != -1) and ((puzzle[i]%100) >= 10)}
    allow
    {
      puzzle[i] = selectedNumber+10;
    }
    label {"Place " i}
  }
  
  // The hard bit
  when {selectedPuzzle != -1}
  allow
  {
    // Check is toggleable
    checkOn = 1 - checkOn;
    
    if (checkOn == 1)
    {
      // Do check
      // Be optimistic...
      puzzleComplete = 1;
      
      // Rows
      for i (0 .. 8)
      {
        for j (0 .. 9)
          {numberCount[j] = 0;}
        numberCount[0] += 1;
        
        for j (0 .. 8)
          {numberCount[puzzle[i*9 + j]%10] += 1;}
        for j (0 .. 8)
        {
          if ((numberCount[puzzle[i*9 + j]%10] > 1) and (puzzle[i*9 + j] < 100))
          {
            puzzle[i*9 + j] += 100;
            puzzleComplete = 0;
          }
        }
      }
      // Columns
      for i (0 .. 8)
      {
        for j (0 .. 9)
          {numberCount[j] = 0;}
        numberCount[0] += 1;
        
        for j (0 .. 8)
          {numberCount[puzzle[j*9 + i]%10] += 1;}
        for j (0 .. 8)
        {
          if ((numberCount[puzzle[j*9 + i]%10] > 1) and (puzzle[j*9 + i] < 100))
          {
            puzzle[j*9 + i] += 100;
            puzzleComplete = 0;
          }
        }
      }
      // Sub-squares
      for i (0 .. 8)
      {
        for j (0 .. 9)
          {numberCount[j] = 0;}
        numberCount[0] += 1;
        
        for j (0 .. 8)
          {numberCount[puzzle[SubSquareStart[i] + ((j/3)*9) + (j%3)]%10] += 1;}
        for j (0 .. 8)
        {
          if ((numberCount[puzzle[SubSquareStart[i] + ((j/3)*9) + (j%3)]%10] > 1)
                 and (puzzle[SubSquareStart[i] + ((j/3)*9) + (j%3)] < 100))
          {
            puzzle[SubSquareStart[i] + ((j/3)*9) + (j%3)] += 100;
            puzzleComplete = 0;
          }
        }
      }
    }
    if (checkOn != 1)
    {
      // Clear the checks
      puzzleComplete = 0;
      for i (0 .. 8)
      {
        for j (0 .. 8)
        {
          if (puzzle[(i*9) + j] > 100)
            {puzzle[(i*9) + j] -= 100;}
        }
      }
    }
  }
  label {"Check"}
}

display
{
  // Display
  box {content {"Sudoku v0.2"}}
  newline
  
  // Draw the puzzles that can be chosen
  for i (0 .. numPuzzles - 1)
  {
    box
    {
      bind_action {"Picked puzzle " i}
      
      while_active
      {
        border { 1px solid black }
        content { "Puzzle " (i+1) " - Difficulty " puzzles[i*(9*9+1)] }
      }
    }
    newline
  }
  
  // If they have succeeded...
  box
  {
    if (puzzleComplete == 1)
    {
      border { 1px solid black }
      background { #aaf }
      content {"Congratulations!"}
    }
  }
  newline
  box {if (puzzleComplete == 1) {content{" "}}}
  newline
  
  // Draw the board, assigning all the possible actions as we go
  for y ( 0 .. 8 )
  {
    for x ( 0 .. 8 )
    {
      box
      {
        if (selectedPuzzle != -1)
        {
          border { 1px solid black }
          width { 2em }
          height { 2em }
          background { #fff }
          if (puzzle[(y*9)+x]%10 == 0)
            {content {" "}}
          if (puzzle[(y*9)+x]%10 != 0)
            {content {puzzle[(y*9)+x]%10}}
          
          bind_action { "Place " ((y*9)+x) }
          while_active
            {background { #aaf }}
          
          if (puzzle[(y*9)+x] > 100)
            {border { 1px solid red }}
        }
      }
    }
    newline
  }
  newline
  
  box {content {" "}}
  newline

  for i ( 0 .. 9 )
  {
    box
    {
      bind_action { "Selected " i }
      while_active
      {
        border { 1px solid black }
        width { 2em }
        height { 2em }
        background { #fff }
        if (i == 0)
          {content {" "}}
        if (i != 0)
          {content {i}}
        background { #fff }
        
        if (i == selectedNumber)
          {background { #aaf }}
      }
    }
  }
  newline
  
  box {if (selectedPuzzle != -1) {content{" "}}}
  newline
  
  box
  {
    bind_action {"Check"}
    while_active
    {
      border { 1px solid black }
      content {"Check Puzzle"}
      if (checkOn == 1) {content {"Clear Check"}}
    }
  }
  newline
  
  box {if (selectedPuzzle != -1) {content{" "}}}
  newline
}


ec2-18-118-200-86.us-east-2.compute.amazonaws.com | ToothyWiki | Kazuhiko | RecentChanges | Login | Webcomic
This page is read-only | View other revisions | Recently used referrers
Last edited February 14, 2006 7:31 am (viewing revision 4, which is the newest) (diff)
Search: