示例#1
0
 static void SkipLabel()
 {
     do
     {
         ch = src.ReadChar();
         if (ch == ';')
         {
             src.ReadLn();    // ignore comments
         }
     } while (!(src.EOF() || char.IsLetter(ch)));
 }
示例#2
0
        public static bool Assemble(string sourceName)
        {
            // Assembles source code from an input file sourceName and loads codeLen
            // words of code directly into memory PVM.mem[0 .. codeLen-1],
            // storing strings in the string pool at the top of memory in
            // PVM.mem[stkBase .. memSize-1].
            //
            // Returns true if assembly succeeded
            //
            // Instruction format :
            //    Instruction  = [ Label ] Opcode [ AddressField ] [ Comment ]
            //    Label        = [ "{" ] Integer [ "}" ]
            //    Opcode       = Mnemonic
            //    AddressField = Integer | "String"
            //    Comment      = String
            //
            // A string AddressField may only be used with a PRNS opcode
            // Instructions are supplied one to a line; terminated at end of input file

            string mnemonic; // mnemonic for matching
            char   quote;    // string delimiter

            src = new InFile(sourceName);
            if (src.OpenError())
            {
                Console.WriteLine("Could not open input file\n");
                System.Environment.Exit(1);
            }
            Console.WriteLine("Assembling code ... \n");
            codeLen = 0;
            stkBase = PVM.memSize - 1;
            okay    = true;
            do
            {
                SkipLabel();                         // ignore labels at start of line
                if (!src.EOF())                      // we must have a line
                {
                    mnemonic = ReadMnemonic();       // unpack mnemonic
                    if (mnemonic == null)
                    {
                        continue;                    // ignore directives
                    }
                    int op = PVM.OpCode(mnemonic);   // look it up
                    PVM.mem[codeLen] = op;           // store in memory
                    switch (op)
                    {
                    case PVM.prns:                   // requires a string address field
                        do
                        {
                            quote = src.ReadChar();  // search for opening quote character
                        }while (quote != '"' && quote != '\'' && quote != '\n');
                        codeLen          = (codeLen + 1) % PVM.memSize;
                        PVM.mem[codeLen] = stkBase - 1; // pointer to start of string
                        if (quote == '\n')
                        {
                            Error("Missing string address", codeLen);
                        }
                        else                         // stack string in literal pool
                        {
                            ch = src.ReadChar();
                            while (ch != quote && ch != '\n')
                            {
                                if (ch == '\\')
                                {
                                    ch = src.ReadChar();
                                    if (ch == '\n')  // closing quote missing
                                    {
                                        Error("Malformed string", codeLen);
                                    }
                                    switch (ch)
                                    {
                                    case 't': ch = '\t'; break;

                                    case 'n': ch = '\n'; break;

                                    case '\"': ch = '\"'; break;

                                    case '\'': ch = '\''; break;

                                    default: break;
                                    }
                                }
                                stkBase--;
                                PVM.mem[stkBase] = ch;
                                ch = src.ReadChar();
                            }
                            if (ch == '\n')          // closing quote missing
                            {
                                Error("Malformed string", codeLen);
                            }
                        }
                        stkBase--;
                        PVM.mem[stkBase] = 0;        // terminate string with nul
                        break;

                    case PVM.brn:                    // all require numeric address field
                    case PVM.bze:
                    case PVM.dsp:
                    case PVM.lda:
                    case PVM.ldc:
                        codeLen = (codeLen + 1) % PVM.memSize;
                        if (ch == '\n')              // no field could be found
                        {
                            Error("Missing address", codeLen);
                        }
                        else                         // unpack it and store
                        {
                            PVM.mem[codeLen] = src.ReadInt();
                            if (src.Error())
                            {
                                Error("Bad address", codeLen);
                            }
                        }
                        break;

                    case PVM.nul:                    // unrecognized mnemonic
                        Console.Write(mnemonic);
                        Error(" - Invalid opcode", codeLen);
                        break;

                    default:                         // no address needed
                        break;
                    }
                    if (!src.EOL())
                    {
                        src.ReadLn();                // skip comments
                    }
                    codeLen = (codeLen + 1) % PVM.memSize;
                }
            } while (!src.EOF());
            for (int i = codeLen; i < stkBase; i++)  // fill with invalid OpCodes
            {
                PVM.mem[i] = PVM.nul;
            }
            return(okay);
        }
示例#3
0
        public static bool Assemble(string sourceName)
        {
            string mnemonic; // mnemonic for matching
            char   quote;    // string delimiter

            src = new InFile(sourceName);
            if (src.OpenError())
            {
                Console.WriteLine("Could not open input file\n");
                System.Environment.Exit(1);
            }
            Console.WriteLine("Assembling code ... \n");
            codeLen = 0;
            stkBase = PVM.memSize - 1;
            okay    = true;
            do
            {
                SkipLabel();                         // ignore labels at start of line
                if (!src.EOF())                      // we must have a line
                {
                    mnemonic = ReadMnemonic();       // unpack mnemonic
                    if (mnemonic == null)
                    {
                        continue;                    // ignore directives
                    }
                    int op = PVM.OpCode(mnemonic);   // look it up
                    PVM.mem[codeLen] = op;           // store in memory
                    switch (op)
                    {
                    case PVM.prns:                   // requires a string address field
                        do
                        {
                            quote = src.ReadChar();  // search for opening quote character
                        }while (quote != '"' && quote != '\'' && quote != '\n');
                        codeLen          = (codeLen + 1) % PVM.memSize;
                        PVM.mem[codeLen] = stkBase - 1; // pointer to start of string
                        if (quote == '\n')
                        {
                            Error("Missing string address", codeLen);
                        }
                        else                         // stack string in literal pool
                        {
                            ch = src.ReadChar();
                            while (ch != quote && ch != '\n')
                            {
                                if (ch == '\\')
                                {
                                    ch = src.ReadChar();
                                    if (ch == '\n')  // closing quote missing
                                    {
                                        Error("Malformed string", codeLen);
                                    }
                                    switch (ch)
                                    {
                                    case 't': ch = '\t'; break;

                                    case 'n': ch = '\n'; break;

                                    case '\"': ch = '\"'; break;

                                    case '\'': ch = '\''; break;

                                    default: break;
                                    }
                                }
                                stkBase--;
                                PVM.mem[stkBase] = ch;
                                ch = src.ReadChar();
                            }
                            if (ch == '\n')          // closing quote missing
                            {
                                Error("Malformed string", codeLen);
                            }
                        }
                        stkBase--;
                        PVM.mem[stkBase] = 0;        // terminate string with nul
                        break;

                    case PVM.brn:                    // all require numeric address field
                    case PVM.bze:
                    case PVM.dsp:
                    case PVM.lda:
                    case PVM.ldc:
                    case PVM.ldl:
                    case PVM.stl:
                        codeLen = (codeLen + 1) % PVM.memSize;
                        if (ch == '\n')              // no field could be found
                        {
                            Error("Missing address", codeLen);
                        }
                        else                         // unpack it and store
                        {
                            PVM.mem[codeLen] = src.ReadInt();
                            if (src.Error())
                            {
                                Error("Bad address", codeLen);
                            }
                        }
                        break;

                    case PVM.nul:                    // unrecognized mnemonic
                        Console.Write(mnemonic);
                        Error(" - Invalid opcode", codeLen);
                        break;

                    default:                         // no address needed
                        break;
                    }
                    if (!src.EOL())
                    {
                        src.ReadLn();                // skip comments
                    }
                    codeLen = (codeLen + 1) % PVM.memSize;
                }
            } while (!src.EOF());
            for (int i = codeLen; i < stkBase; i++)  // fill with invalid OpCodes
            {
                PVM.mem[i] = PVM.nul;
            }
            return(okay);
        }
示例#4
0
        /// <summary>
        /// Main program
        /// </summary>

        private static void Main(string[] args)
        {
            int[,] board = new int[9, 9];
            // Check if a file input is specified
            if (args.Length == 0)
            {
                //some input will be one of the solution files e.g s1
                IO.WriteLine("Nothing was specified for input, rather used \"SudokuPrac1Question12.exe <some input>\"");
                IO.ReadLine();
                return;
            }

            // Load the file
            InFile file = new InFile(args[0]);

            string[] file_lines      = new string[21];
            int      file_line_count = 0;

            while (!file.EOF())
            {
                file_lines[file_line_count] = file.ReadLine();
                file_line_count++;
            }

            // Interpret starting board: first 9 lines
            for (int i = 0; i < 9; i++)
            {
                var points = file_lines[i].Split(" ".ToCharArray(), System.StringSplitOptions.RemoveEmptyEntries);
                for (int j = 0; j < 9; j++)
                {
                    board[j, i] = int.Parse(points[j]);
                }
            }

            // Initiate the board
            CreatePositionPoints();
            CreatePlanningBoard(board);

            bool not_complete = true, endGame = false;
            int  pos_x = 0, pos_y = 0, pos_choice = 0, value_count = 1;

            int[,] ai_chosen_spots = new int[9, 9];
            //main game loop
            while (not_complete && !endGame)
            {
                Console.Clear();
                UpdateSuggestedMoves(board, ref planning_board);

                for (int x = 0; x < 9; x++)
                {
                    for (int y = 0; y < 9; y++)
                    {
                        ai_chosen_spots[x, y] = 0;
                    }
                }

                DrawPlanningBoard();
                int knownCount = 0;
                for (int x = 0; x < 9; x++)
                {
                    for (int y = 0; y < 9; y++)
                    {
                        if (board[x, y] != 0)
                        {
                            knownCount++;
                        }
                    }
                }
                AIGenerateChoices(ref ai_chosen_spots, board, StringPositions);

                //Predicate<int> pre = delegate (int a) { return a == 0; };
                int predictionCount = 0;
                for (int x = 0; x < 9; x++)
                {
                    for (int y = 0; y < 9; y++)
                    {
                        if (ai_chosen_spots[x, y] != 0)
                        {
                            predictionCount++;
                        }
                    }
                }

                if (predictionCount != 0) // if we found an ai choice
                {
                    DrawBoard(board, ai_chosen_spots);

                    IO.WriteLine(knownCount + " known " + predictionCount + " predicted\n");

                    // check if at least 1 is suggested
                    IO.WriteLine("I did these, so press <enter> once:");
                    IO.ReadLine();

                    /*if (playerinput=1){
                     * //AIFillIn();
                     *    }
                     */
                }
                else
                {
                    DrawBoard(board);
                    GetPlayerInput(ref pos_x, ref pos_y, ref pos_choice, ref endGame);
                    if (IsValidMove(board, pos_x, pos_y, pos_choice))
                    {
                        PlaceMoveAtPos(ref board, pos_x, pos_y, pos_choice);

                        UpdateSuggestedMoves(board, ref planning_board);
                    }
                    else
                    {
                        IO.WriteLine("Move is not valid, please try a new one");
                    }
                }
                // board complete check
                value_count = 0;
                for (int i = 0; i < 9; i++)
                {
                    for (int j = 0; j < 9; j++)
                    {
                        if (board[i, j] == 0)
                        {
                            ++value_count;
                        }
                    }
                }
                if (value_count == 0)
                {
                    IO.WriteLine("Game won!");
                    not_complete = false;
                }
            }

            IO.ReadLine();
        }