Maybe for the AI, have the program detect which buttons have already been used; then just have it pick one of the buttons that haven't been used. The only hard part with this is having it "pick". It could randomly generate a number between 1 and 9, (lets say it picks 7) then it checks if button 7 is already occupied, if it is, it generates a new random number (over and over again).